Fix getuidvalidity crash (UIDVALIDITY returning None)

Rename getuidvalidity -> get_uidvalidity and cache the IMAP result.

1) Start modernizing our function names using more underscores
2) IMAPs implementation of get_uidvalidity was removing the UIDVALIDITY result
   from the imaplib2 result stack, so subsequent calls would return "None".
   As various functions can invoke this, this led to some errors that we
   avoid by caching the current UIDVALIDITY value in the Folder instance.

There are more simplifications and improvements to be made.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth
2012-01-19 11:24:16 +01:00
parent 7c4fea906f
commit 9453e1d955
7 changed files with 34 additions and 20 deletions

View File

@ -65,17 +65,23 @@ class IMAPFolder(BaseFolder):
def getcopyinstancelimit(self):
return 'MSGCOPY_' + self.repository.getname()
def getuidvalidity(self):
def get_uidvalidity(self):
"""Retrieve the current connections UIDVALIDITY value
UIDVALIDITY value will be cached on the first call.
:returns: The UIDVALIDITY as (long) number."""
if hasattr(self, '_uidvalidity'):
# use cached value if existing
return self._uidvalidity
imapobj = self.imapserver.acquireconnection()
try:
# SELECT receives UIDVALIDITY response
# SELECT (if not already done) and get current UIDVALIDITY
self.selectro(imapobj)
# note: we would want to use .response() here but that
# often seems to return [None], even though we have
# data. TODO
uidval = imapobj._get_untagged_response('UIDVALIDITY')
assert uidval != [None], "response('UIDVALIDITY') returned [None]!"
return long(uidval[-1])
typ, uidval = imapobj.response('UIDVALIDITY')
assert uidval != [None] and uidval != None, \
"response('UIDVALIDITY') returned [None]!"
self._uidvalidity = long(uidval[-1])
return self._uidvalidity
finally:
self.imapserver.releaseconnection(imapobj)