Issue 22319: mailbox.MH chokes on directories without .mh_sequences (original) (raw)
If a mailbox.MH() object is created by pointing at a path that exists but doesn't contain a ".mh_sequences" file, it raises an exception upon iteration over .{iter,}items() rather than gracefully assuming that the file is empty. I encountered this by pointing it at a Claws Mail IMAP-cache folder (which claims to store its messages in MH format¹ but it doesn't place a .mh_sequences file in those folders) only to have it raise an exception.
To replicate: $ mkdir empty $ python
import mailbox for msg in mailbox.MH('empty').values(): pass
I suspect this could simply wrap the "f = open(os.path.join(self._path, '.mh_sequences'), 'r')" and following lines in a check to ignore the file if it doesn't exist (returning the empty "results").
¹ http://www.claws-mail.org/faq/index.php/General_Information#How_does_Claws_Mail_store_mails.3F
I had to tweak the example reproduction code as it seemed to succeed (i.e., fail to demonstrate the problem) in some instances. The same exception occurs, but here's the full original traceback:
$ cd /home/tim/.claws-mail/imapcache/mail.example.com/tim@example.com/INBOX/
$ python3 Python 3.2.3 (default, Feb 20 2013, 14:44:27) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.
import mailbox m = mailbox.MH('.') for msg in m: ... print(msg) ... Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.2/mailbox.py", line 114, in itervalues value = self[key] File "/usr/lib/python3.2/mailbox.py", line 78, in getitem return self.get_message(key) File "/usr/lib/python3.2/mailbox.py", line 1019, in get_message for name, key_list in self.get_sequences().items(): File "/usr/lib/python3.2/mailbox.py", line 1128, in get_sequences f = open(os.path.join(self._path, '.mh_sequences'), 'r') IOError: [Errno 2] No such file or directory: '/home/tim/.claws-mail/imapcache/mail.example.com/tim@example.com/INBOX/.mh_sequences'