Skip to content
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

AttributeError in email.message.iter_attachments() #78153

Closed
skrohlas mannequin opened this issue Jun 26, 2018 · 9 comments
Closed

AttributeError in email.message.iter_attachments() #78153

skrohlas mannequin opened this issue Jun 26, 2018 · 9 comments
Labels
3.9 only security fixes topic-email type-bug An unexpected behavior, bug, or error

Comments

@skrohlas
Copy link
Mannequin

skrohlas mannequin commented Jun 26, 2018

BPO 33972
Nosy @warsaw, @bitdancer, @miss-islington, @stoksc, @iritkatriel, @xkobal
PRs
  • bpo-33972: MIMEPart.iter_attachments blows up more gracefully when co… #8041
  • bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError. #14119
  • [3.8] bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError (GH-14119) #14380
  • [3.7] bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError. (GH-14119) #14381
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-10-19.21:50:00.737>
    created_at = <Date 2018-06-26.14:43:31.432>
    labels = ['type-bug', 'expert-email', '3.9']
    title = 'AttributeError in email.message.iter_attachments()'
    updated_at = <Date 2021-01-11.14:51:45.854>
    user = 'https://bugs.python.org/skrohlas'

    bugs.python.org fields:

    activity = <Date 2021-01-11.14:51:45.854>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-10-19.21:50:00.737>
    closer = 'barry'
    components = ['email']
    creation = <Date 2018-06-26.14:43:31.432>
    creator = 'skrohlas'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33972
    keywords = ['patch']
    message_count = 9.0
    messages = ['320494', '320852', '346540', '346546', '346547', '378378', '379028', '384835', '384837']
    nosy_count = 7.0
    nosy_names = ['barry', 'r.david.murray', 'miss-islington', 'skrohlas', 'stoksc', 'iritkatriel', 'xavier2']
    pr_nums = ['8041', '14119', '14380', '14381']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue33972'
    versions = ['Python 3.9']

    @skrohlas
    Copy link
    Mannequin Author

    skrohlas mannequin commented Jun 26, 2018

    Hello everyone,

    Today I stumbled over unexpected behaviour when parsing emails with Python 3.5.

    mail is of type: <class 'email.message.EmailMessage'>

    Traceback (most recent call last):
    File "XXX", line YYY, in ZZZ
    for attachment in mail.iter_attachments():
    File "/usr/lib/python3.5/email/message.py", line 1025, in iter_attachments
    parts = self.get_payload().copy()
    AttributeError: 'str' object has no attribute 'copy'

    /usr/lib/python3.5/email/message.py calls self.get_payload().copy() without distinguishing between different possible return types of get_payload().

    get_payload() belongs to class MIMEPart(Message)

    From the (base) class Message:

    "def get_payload(self, i=None, decode=False):
    """Return a reference to the payload.

    The payload will either be a list object or a string."

    So, it might return a string, which seems to be the case I'm hitting here.

    The code in cpython trunk still looks identical apart from a few line offsets, so newer versions might be affected, too.

    Am I doing something wrong or do we have a bug to squash here?

    Thanks,
    Sven

    @skrohlas skrohlas mannequin added type-crash A hard crash of the interpreter, possibly with a core dump topic-email labels Jun 26, 2018
    @stoksc
    Copy link
    Mannequin

    stoksc mannequin commented Jul 2, 2018

    Hey Sven,

    New here, decided to try a bit at an issue.

    According to the email.message docs, "the payload is either a string or bytes object, in the case of simple message objects, or a list of EmailMessage objects, for MIME container documents such as multipart/* and message/rfc822 message objects."

    Like you said, this error happens if the email.message.EmailMessage object is bad, i.e. the payload is a string or bytes but the content-type is marked as multipart/*. After reading online, it seems like an error is necessary.

    I've proposed a small change in a pull request to make this situation raise a more useful error. It doesn't blow up any earlier but it at least says something more useful.

    The function could return instead of throwing an error, just providing an empty iterator, but that seems even less useful.

    I fixed it on master (3.8 dev or whatever).

    Best,
    Brad

    @stoksc stoksc mannequin added the 3.8 only security fixes label Jul 2, 2018
    @serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Jul 11, 2018
    @warsaw
    Copy link
    Member

    warsaw commented Jun 25, 2019

    New changeset 0225701 by Barry Warsaw (Abhilash Raj) in branch 'master':
    bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError. (GH-14119)
    0225701

    @miss-islington
    Copy link
    Contributor

    New changeset c6e3282 by Miss Islington (bot) (Abhilash Raj) in branch '3.8':
    [3.8] bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError (GH-14119) (GH-14380)
    c6e3282

    @miss-islington
    Copy link
    Contributor

    New changeset 01b2394 by Miss Islington (bot) (Abhilash Raj) in branch '3.7':
    [3.7] bpo-33972: Fix EmailMessage.iter_attachments raising AttributeError. (GH-14119) (GH-14381)
    01b2394

    @iritkatriel
    Copy link
    Member

    This seems complete, can it be closed?

    @warsaw
    Copy link
    Member

    warsaw commented Oct 19, 2020

    Thank you all for the fix. Closing.

    @warsaw warsaw closed this as completed Oct 19, 2020
    @xkobal
    Copy link
    Mannequin

    xkobal mannequin commented Jan 11, 2021

    The problem still occurs with the _find_body
    method. The "part.is_attachment()" method can trigger an AttributeError too "AttributeError: 'str' object has no attribute 'is_attachment'"

    https://github.com/python/cpython/blob/3.9/Lib/email/message.py#L978

    @xkobal xkobal mannequin added 3.9 only security fixes and removed 3.8 only security fixes labels Jan 11, 2021
    @iritkatriel
    Copy link
    Member

    Xavier, I think it would be best if you could open a new issue for that, and also include code to reproduce the problem.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes topic-email type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants