Close Cursor objects explicitly

This commit is contained in:
Stuart Gathman
2013-02-17 05:13:38 +00:00
parent dd581f5d9a
commit ff06b5f1b4
+43 -39
View File
@@ -26,50 +26,54 @@ class Greylist(object):
"Delete records past the retention limit." "Delete records past the retention limit."
now = time.time() + timeinc - self.greylist_retain now = time.time() + timeinc - self.greylist_retain
cur = self.conn.cursor() cur = self.conn.cursor()
cur.execute('delete from greylist where lastseen < ?',(now,)) try:
cnt = cur.rowcount cur.execute('delete from greylist where lastseen < ?',(now,))
self.conn.commit() cnt = cur.rowcount
self.conn.commit()
finally: cur.close()
return cnt return cnt
def check(self,ip,sender,recipient,timeinc=0): def check(self,ip,sender,recipient,timeinc=0):
"Return number of allowed messages for greylist triple." "Return number of allowed messages for greylist triple."
cur = self.conn.cursor() cur = self.conn.cursor()
cur.execute('''select firstseen,lastseen,cnt,umis from greylist where try:
ip=? and sender=? and recipient=?''',(ip,sender,recipient)) cur.execute('''select firstseen,lastseen,cnt,umis from greylist where
r = cur.fetchone() ip=? and sender=? and recipient=?''',(ip,sender,recipient))
now = time.time() + timeinc r = cur.fetchone()
cnt = 0 now = time.time() + timeinc
if not r: cnt = 0
cur.execute('''insert into if not r:
greylist(ip,sender,recipient,firstseen,lastseen,cnt,umis) cur.execute('''insert into
values(?,?,?,?,?,?,?)''', (ip,sender,recipient,now,now,0,None)) greylist(ip,sender,recipient,firstseen,lastseen,cnt,umis)
elif now > r['lastseen'] + self.greylist_retain: values(?,?,?,?,?,?,?)''', (ip,sender,recipient,now,now,0,None))
# expired elif now > r['lastseen'] + self.greylist_retain:
log.debug('Expired greylist: %s:%s:%s',ip,sender,recipient) # expired
cur.execute('''update greylist set firstseen=?,lastseen=?,cnt=?,umis=? log.debug('Expired greylist: %s:%s:%s',ip,sender,recipient)
where ip=? and sender=? and recipient=?''', cur.execute('''update greylist set firstseen=?,lastseen=?,cnt=?,umis=?
(now,now,0,None,ip,sender,recipient)) where ip=? and sender=? and recipient=?''',
elif now < r['firstseen'] + self.greylist_time + 5: (now,now,0,None,ip,sender,recipient))
# still greylisted elif now < r['firstseen'] + self.greylist_time + 5:
log.debug('Early greylist: %s:%s:%s',ip,sender,recipient) # still greylisted
#r = Record() log.debug('Early greylist: %s:%s:%s',ip,sender,recipient)
cur.execute('''update greylist set lastseen=? #r = Record()
where ip=? and sender=? and recipient=?''', cur.execute('''update greylist set lastseen=?
(now,ip,sender,recipient)) where ip=? and sender=? and recipient=?''',
elif r['cnt'] or now < r['firstseen'] + self.greylist_expire: (now,ip,sender,recipient))
# in greylist window or active elif r['cnt'] or now < r['firstseen'] + self.greylist_expire:
cnt = r['cnt'] + 1 # in greylist window or active
cur.execute('''update greylist set lastseen=?,cnt=? cnt = r['cnt'] + 1
where ip=? and sender=? and recipient=?''', cur.execute('''update greylist set lastseen=?,cnt=?
(now,cnt,ip,sender,recipient)) where ip=? and sender=? and recipient=?''',
log.debug('Active greylist(%d): %s:%s:%s',cnt,ip,sender,recipient) (now,cnt,ip,sender,recipient))
else: log.debug('Active greylist(%d): %s:%s:%s',cnt,ip,sender,recipient)
# passed greylist window else:
log.debug('Late greylist: %s:%s:%s',ip,sender,recipient) # passed greylist window
cur.execute('''update greylist set firstseen=?,lastseen=?,cnt=?,umis=? log.debug('Late greylist: %s:%s:%s',ip,sender,recipient)
where ip=? and sender=? and recipient=?''', cur.execute('''update greylist set firstseen=?,lastseen=?,cnt=?,umis=?
(now,now,0,None,ip,sender,recipient)) where ip=? and sender=? and recipient=?''',
self.conn.commit() (now,now,0,None,ip,sender,recipient))
self.conn.commit()
finally: cur.close()
return cnt return cnt
def close(self): def close(self):