imaplib : error reporting use 'randomly' exception or return value

aspineux aspineux at gmail.com
Thu Feb 1 17:28:17 EST 2007


imaplib use exception to report errors,  but some problems must be
detected by checking the return value !
For example, when trying to append into a mailbox with wrong ACL,
imaplib return 'NO', but dont raise any exception (I give a sample at
the end).
This make error handling more complicate, because any imap statement
is supposed to be followed by a test of the returned value!

Why not report all problems using exceptions ?

It easy to modify imaplib.py to manage this because most of the imap
call are made through function
_simple_command this way :

    def _simple_command(self, name, *args):
        return self._command_complete(name, self._command(name,
*args))

I propose to replace it by something like :

    def _simple_command(self, name, *args):
        typ, dat=self._command_complete(name, self._command(name,
*args))
        if typ!='OK':
            raise self.error(dat[-1])
        return typ, dat

Any comment ?

Here is an example, append  on a mailbox with the wrong ACL fail by
returning a 'NO'

import imaplib

server='localhost'
login='test at asxnet.loc'
passwd='password'

c=imaplib.IMAP4(server)
c.login(login, passwd)
c.setacl('INBOX', login, '') # set wrong ACL, removing 'i'
typ, dat=c.append('INBOX', None, None, "From: foo at bar.com\nTo: %s
\nSubject: test append\n\nHello\n" % (login))
print typ, dat

output:

NO ['Permission denied']




More information about the Python-list mailing list