TeamMsgExtractor / msg-extractor

Extracts emails and attachments saved in Microsoft Outlook's .msg files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AttributeError: 'MSGFile' object has no attribute '_MSGFile__open'

moyoakala opened this issue · comments

Bug Metadata

  • Version of extract_msg: [0.39.0]
  • Your python version: Python [3.10.7]
  • How did you launch extract_msg?
    • My command line or
    • I used the extract_msg package

Describe the bug
An attribute error is pops up in my debugger (vscode IDE) whenever the MSGFile class is called. I suppose this happens when I try to close the connection in a 'with' statement for example. The exception is an ignored one so it doesnt break the functionality. I just can't seem to figure out why it pops up.

**What code did you use or can we use to reproduce this error?

with extract_msg.openMsg(received_data) as msgfile:
    msgfile.saveAttachments(customPath=target_directory, useMsgFilename=True, attachmentsOnly=True)

or

with extract_msg.MSGFile(filename) as msgfile:
    is_msgfile = msgfile

test_outlook_archive.zip

Is there a message.msg file you want to share to help us reproduce this?

  • Uploaded message (drag and drop on this window)
  • Emailed message as an attachment to admins: [Enter Subject Line Here]

Traceback

Exception ignored in: <function MSGFile.__del__ at 0x7f23d0b9bf40>
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/extract_msg/msg.py", line 158, in __del__
    self.close()
  File "/usr/local/lib/python3.10/site-packages/extract_msg/msg.py", line 427, in close
    if self.__open:
AttributeError: 'MSGFile' object has no attribute '_MSGFile__open'
Exception ignored in: <function MSGFile.__del__ at 0x7f23d0b9bf40>
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/extract_msg/msg.py", line 158, in __del__
    self.close()
  File "/usr/local/lib/python3.10/site-packages/extract_msg/msg.py", line 427, in close
    if self.__open:
AttributeError: 'MSGFile' object has no attribute '_MSGFile__open'

Additional context
I uploaded a test file as a compressed file (.zip) since github doesnt support uploading .msg file. Please extract and use.

Frankly I'm not sure how it pops up either, since every path in init should lead to that variable existing. Only thing I can think of is that an exception is happening in init or enter or something, triggering __del__, which then doesn't actually see the variable when it calls exit. Alternatively, the state of the object is bad when the function is run, to the point where that variable has been deleted from the instance somehow.

I can also think of it possibly being an issue that it's being called on program termination and enough data has been destroyed that it is erroring. I can take a look at it later.

Honestly it was just put in to try and ensure certain cleanup would happen if all references to an MSGFile instance were lost before closing it, but I might just remove it entirely because it looks to be more trouble than it's worth 😅

Ah I see. What version before the cleanup was added can I try out?

0.38.0 is the last version without it, though it had some bugs that were fixed along side adding that in the next version. I'll either try to find a way to write it safely so that I don't have to worry about those issues or just remove it entirely.

I think the issue may be related to name mangling (https://peps.python.org/pep-0008/#method-names-and-instance-variables) of the attribute, perhaps using single underscore prefixed attributes (_open) instead of two (__open) would fix it, making it easier for subclasses to correctly reference the attribute. Or ensure that the subclasses also have a __open property set to the value of _MSGFile__open.

I think the issue may be related to name mangling (https://peps.python.org/pep-0008/#method-names-and-instance-variables) of the attribute, perhaps using single underscore prefixed attributes (_open) instead of two (__open) would fix it, making it easier for subclasses to correctly reference the attribute. Or ensure that the subclasses also have a __open property set to the value of _MSGFile__open.

The class referencing it is the same class that uses it. The only class that sets open is MSGFile and the only one with a __del__ method (and a close method which is what checks __open) is MSGFile.

__del__ only calls the close method, and the close method is fully functional.

Looking at some documentation, it looks like the most likely cause of the issue is the order that things are being deleted, which causes some parts of the instance to not be there while __del__ is running, most likely because it was running at interpreter shutdown.

Additionally, the variable isn't meant to be checked by subclasses at all, as the MSGFile class is the part that should be handling the file operations like opening and closing.

Regardless, the method is being deleted because it's honestly more trouble than it's worth. If I can ensure it's functional at all times in the future, and think it's valuable enough to do, then it'll come back in a later update.