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

aspineux aspineux at gmail.com
Fri Feb 2 14:40:59 EST 2007


I looked carefully imaplib.py and wrote this version of
_simple_command that take care of the way the function is used by
other to keep the same functionality

    class bye(imaplib.IMAP4.abort): pass
    class bad(imaplib.IMAP4.abort): pass

    def _simple_command(self, name, *args):

        typ, dat=self._command_complete(name, self._command(name,
*args))
        if typ!='OK':
            if name in ('LOGOUT',):
                return typ, dat

            if name in ('EXAMINE', 'SELECT'):
                self.state = 'AUTH'

            if typ=='BYE': raise self.bye(dat[-1])
            elif typ=='BAD': raise self.bad(dat[-1])
            else: raise self.error(dat[-1])

        return typ, dat


On 1 fév, 23:28, "aspineux" <aspin... at gmail.com> wrote:
> 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='t... 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: f... 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