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

A

aspineux

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='(e-mail address removed)'
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: (e-mail address removed)\nTo: %s
\nSubject: test append\n\nHello\n" % (login))
print typ, dat

output:

NO ['Permission denied']
 
A

aspineux

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top