Skip to content

bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError.#14119

Merged
warsaw merged 4 commits into
python:masterfrom
maxking:bpo-33972
Jun 25, 2019
Merged

bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError.#14119
warsaw merged 4 commits into
python:masterfrom
maxking:bpo-33972

Conversation

@maxking

@maxking maxking commented Jun 15, 2019

Copy link
Copy Markdown
Contributor

When certain malformed messages have content-type set to 'mulitpart/*' but
still have a single part body, iter_attachments can raise AttributeError. This
patch fixes it by returning a None value instead when the body is single part.

https://bugs.python.org/issue33972

When certain malformed messages have content-type set to 'mulitpart/*' but
still have a single part body, iter_attachments can raise AttributeError. This
patch fixes it by returning a None value instead when the body is single part.
@maxking

maxking commented Jun 15, 2019

Copy link
Copy Markdown
Contributor Author

It looks like Docs builds are failing on both Azure and Travis, even though I haven't changed anything in there.

@brettcannon brettcannon added the type-bug An unexpected behavior, bug, or error label Jun 21, 2019
@brettcannon

Copy link
Copy Markdown
Member

@maxking docs look fine now.

@maxking

maxking commented Jun 21, 2019

Copy link
Copy Markdown
Contributor Author

Yep, I am not sure what was the reason for it. I moved multiipart/* (invalid rst?) text to multipart/* text, which fixed the tests :)

Or it could be unrelated, I am not sure.

Comment thread Lib/email/message.py Outdated
# Certain malformed messages can have content type set to `multipart/*`
# but still have single part body, in which case payload.copy() can
# fail with AttributeError.
if not isinstance(payload, list):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What do you think about catching the AttributeError on .copy() instead of doing a type test here? E.g.:

        try:
            parts = payload.copy()
        except AttributeError:
            return None

? (Also aside, when returning None is part of the API, it's generally better to be explicit.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll update to catch AttributeError.

It technically doesn't return None, since this is a generator. I don't know if it that makes any sense?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is same what other return statements in this method do.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Cool, thanks! I didn't pick that up from the diff context.

@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@warsaw warsaw merged commit 0225701 into python:master Jun 25, 2019
maxking added a commit to maxking/cpython-1 that referenced this pull request Jun 25, 2019
…rror. (pythonGH-14119)

* bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError.

When certain malformed messages have content-type set to 'mulitpart/*' but
still have a single part body, iter_attachments can raise AttributeError. This
patch fixes it by returning a None value instead when the body is single part.
(cherry picked from commit 0225701)

Co-authored-by: Abhilash Raj <maxking@users.noreply.github.com>
maxking added a commit to maxking/cpython-1 that referenced this pull request Jun 25, 2019
…rror. (pythonGH-14119)

* bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError.

When certain malformed messages have content-type set to 'mulitpart/*' but
still have a single part body, iter_attachments can raise AttributeError. This
patch fixes it by returning a None value instead when the body is single part.
(cherry picked from commit 0225701)

Co-authored-by: Abhilash Raj <maxking@users.noreply.github.com>
miss-islington pushed a commit that referenced this pull request Jun 25, 2019
…rror (GH-14119) (GH-14380)

When certain malformed messages have content-type set to 'mulitpart/*' but
still have a single part body, iter_attachments can raise AttributeError. This
patch fixes it by returning a None value instead when the body is single part.
(cherry picked from commit 0225701)

Co-authored-by: Abhilash Raj <maxking@users.noreply.github.com>



https://bugs.python.org/issue33972
miss-islington pushed a commit that referenced this pull request Jun 25, 2019
…rror. (GH-14119) (GH-14381)

When certain malformed messages have content-type set to 'mulitpart/*' but
still have a single part body, iter_attachments can raise AttributeError. This
patch fixes it by returning a None value instead when the body is single part.
(cherry picked from commit 0225701)

Co-authored-by: Abhilash Raj <maxking@users.noreply.github.com>



https://bugs.python.org/issue33972
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
…ythonGH-14119)

* bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError.

When certain malformed messages have content-type set to 'mulitpart/*' but
still have a single part body, iter_attachments can raise AttributeError. This
patch fixes it by returning a None value instead when the body is single part.
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
…ythonGH-14119)

* bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError.

When certain malformed messages have content-type set to 'mulitpart/*' but
still have a single part body, iter_attachments can raise AttributeError. This
patch fixes it by returning a None value instead when the body is single part.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants