From: Thomas Jarosch Date: Thu, 5 Jul 2012 08:07:49 +0000 (+0200) Subject: Make sure the KeyBoardInterrupt exception reaches main() X-Git-Url: http://developer.intra2net.com/git/?p=imap-fix-internaldate;a=commitdiff_plain;h=8ecad608d0fcc245cd79a71db4c736658f45dc37 Make sure the KeyBoardInterrupt exception reaches main() --- diff --git a/src/mail_iterator.py b/src/mail_iterator.py index c1f2e80..2220a3e 100644 --- a/src/mail_iterator.py +++ b/src/mail_iterator.py @@ -44,13 +44,13 @@ class MailIterator: self.mail_con = imaplib.IMAP4_SSL(server) self.mail_con.login(username, password) logging.info("Logged in as %s.", username) - except: + except (self.mail_con.error): self.logged_in = False raise UserWarning("Could not log in as user " + username + ".") self.logged_in = True try: _result, self.mailboxes = self.mail_con.list() - except: + except (self.mail_con.error): raise UserWarning("Could not retrieve mailboxes for user " + username + ".") self.skip_shared_folders = skip_shared_folders @@ -72,7 +72,7 @@ class MailIterator: # retrieve uidvalidity try: _result, data = self.mail_con.status(mailbox[2], '(UIDVALIDITY)') - except: + except (self.mail_con.error): raise UserWarning("Could not retrieve mailbox uidvalidity.") uidval = UIDVAL_RESP.match(data[0].decode('iso-8859-1')).groups() logging.debug("Extracted mailbox info is %s %s.", data[0], uidval) @@ -80,7 +80,7 @@ class MailIterator: try: self.mail_con.select(mailbox[2]) except self.mail_con.readonly: - logging.warning("Mailbox %s is not writable and therefore skipped.", mailbox[2]) + logging.warning("Mailbox %s is not writable and therefore skipped.", mailbox[2]) continue yield (mailbox[2], uidval[1]) @@ -88,7 +88,7 @@ class MailIterator: """Fetches the messages from the current mailbox, return list of uids.""" try: _result, data = self.mail_con.uid('search', None, "ALL") - except: + except (self.mail_con.error): raise UserWarning("Could not fetch messages.") mailid_list = data[0].split() return mailid_list @@ -97,7 +97,7 @@ class MailIterator: """Fetches the internal date of a message, returns a time tuple.""" try: _result, data = self.mail_con.uid('fetch', mid, '(INTERNALDATE)') - except: + except (self.mail_con.error): raise UserWarning("Could not fetch the internal date of message " + mid.decode('iso-8859-1') + ".") internal_date = imaplib.Internaldate2tuple(data[0]) return internal_date @@ -106,7 +106,7 @@ class MailIterator: """Fetches the received date of a message, returns bytes reponse.""" try: _result, data = self.mail_con.uid('fetch', mid, '(BODY.PEEK[HEADER.FIELDS (RECEIVED)])') - except: + except (self.mail_con.error): raise UserWarning("Could not fetch the received header of message " + mid.decode('iso-8859-1') + ".") return data[0][1].decode('iso-8859-1') @@ -114,7 +114,7 @@ class MailIterator: """Fetches the basic date of a message, returns bytes reponse.""" try: _result, data = self.mail_con.uid('fetch', mid, '(BODY.PEEK[HEADER.FIELDS (DATE)])') - except: + except (self.mail_con.error): raise UserWarning("Could not fetch the date header of message " + mid.decode('iso-8859-1') + ".") return data[0][1].decode('iso-8859-1') @@ -139,12 +139,12 @@ class MailIterator: result, data = self.mail_con.append(mailbox, flags_str, internal_date_sec, data[0][1]) logging.debug("Adding corrected copy of the message reponse: %s %s", result, data) - except: + except (self.mail_con.error): raise UserWarning("Could not replace the e-mail " + mid.decode('iso-8859-1') + ".") try: result, data = self.mail_con.uid('STORE', mid, '+FLAGS', r'(\Deleted)') logging.debug("Removing old copy of the message reponse: %s %s", result, data) - except: + except (self.mail_con.error): raise UserWarning("Could not delete the e-mail " + mid.decode('iso-8859-1') + ".") self.mail_con.expunge() return