Skip to content

Commit a44cd1b

Browse files
committed
Don't assume that exception block leaks the caught error (will break in Python 3).
1 parent 6e8ead0 commit a44cd1b

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

DBUtils/SteadyDB.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ def tough_method(*args, **kwargs):
595595
con._store(con2)
596596
self._cursor = cursor2
597597
raise error # raise the original error again
598+
error2 = None
598599
try: # try one more time to execute
599600
if execute:
600601
self._setsizes(cursor2)
@@ -604,19 +605,20 @@ def tough_method(*args, **kwargs):
604605
self._clearsizes()
605606
except error.__class__: # same execution error
606607
use2 = False
608+
error2 = error
607609
except Exception, error: # other execution errors
608610
use2 = True
611+
error2 = error
609612
else:
610613
use2 = True
611-
error = None
612614
if use2:
613615
self.close()
614616
con._close()
615617
con._store(con2)
616618
self._cursor = cursor2
617619
con._usage += 1
618-
if error:
619-
raise error # raise the other error
620+
if error2:
621+
raise error2 # raise the other error
620622
return result
621623
try:
622624
cursor2.close()

DBUtils/Tests/TestPooledDB.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
sys.path.insert(1, '../..')
2323
# The TestSteadyDB module serves as a mock object for the DB-API 2 module:
2424
from DBUtils.Tests import TestSteadyDB as dbapi
25-
from DBUtils.PooledDB import PooledDB, TooManyConnections, SharedDBConnection
25+
from DBUtils.PooledDB import (PooledDB, SharedDBConnection,
26+
InvalidConnection, TooManyConnections)
2627

2728

2829
class TestPooledDB(unittest.TestCase):
@@ -208,7 +209,7 @@ def test04_CloseConnection(self):
208209
if shareable:
209210
self.assertEqual(db._shared_con, None)
210211
self.assertEqual(shared_con.shared, 0)
211-
self.assert_(not hasattr(db, '_usage'))
212+
self.assertRaises(InvalidConnection, getattr, db, '_usage')
212213
self.assert_(not hasattr(db_con, '_num_queries'))
213214
self.assertEqual(len(pool._idle_cache), 1)
214215
self.assertEqual(pool._idle_cache[0]._con, db_con)

DBUtils/Tests/TestPooledPg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
sys.path.insert(1, '../..')
2323
# The TestSteadyPg module serves as a mock object for the pg API module:
2424
from DBUtils.Tests import TestSteadyPg
25-
from DBUtils.PooledPg import PooledPg
25+
from DBUtils.PooledPg import PooledPg, InvalidConnection
2626

2727

2828
class TestPooledPg(unittest.TestCase):
@@ -95,7 +95,7 @@ def test2_CloseConnection(self):
9595
db.query('select test')
9696
self.assertEqual(db.num_queries, 1)
9797
db.close()
98-
self.assert_(not hasattr(db, 'num_queries'))
98+
self.assertRaises(InvalidConnection, getattr, db, 'num_queries')
9999
db = pool.connection()
100100
self.assert_(hasattr(db, 'dbname'))
101101
self.assertEqual(db.dbname, 'PooledPgTestDB')

0 commit comments

Comments
 (0)