mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-26 02:43:43 +03:00
Use modern except syntax throughout project
The syntax "except Exception, exc:" is deprecated. All Python versions supported by psycopg2 support the newer, modern syntax. Forward compatible with future Python versions.
This commit is contained in:
parent
7a2dd85caa
commit
390e43fcb1
|
@ -363,7 +363,7 @@ class NamedTupleCursor(_cursor):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
except ImportError, _exc:
|
except ImportError as _exc:
|
||||||
def _make_nt(self):
|
def _make_nt(self):
|
||||||
raise self._exc
|
raise self._exc
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -65,7 +65,7 @@ def test_suite():
|
||||||
import psycopg2
|
import psycopg2
|
||||||
try:
|
try:
|
||||||
cnn = psycopg2.connect(dsn)
|
cnn = psycopg2.connect(dsn)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
print "Failed connection to test db:", e.__class__.__name__, e
|
print "Failed connection to test db:", e.__class__.__name__, e
|
||||||
print "Please set env vars 'PSYCOPG2_TESTDB*' to valid values."
|
print "Please set env vars 'PSYCOPG2_TESTDB*' to valid values."
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
|
@ -444,7 +444,7 @@ class AsyncTests(ConnectingTestCase):
|
||||||
try:
|
try:
|
||||||
cnn = psycopg2.connect('dbname=thisdatabasedoesntexist', async_=True)
|
cnn = psycopg2.connect('dbname=thisdatabasedoesntexist', async_=True)
|
||||||
self.wait(cnn)
|
self.wait(cnn)
|
||||||
except psycopg2.Error, e:
|
except psycopg2.Error as e:
|
||||||
self.assertNotEqual(str(e), "asynchronous connection failed",
|
self.assertNotEqual(str(e), "asynchronous connection failed",
|
||||||
"connection error reason lost")
|
"connection error reason lost")
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -81,7 +81,7 @@ class AsyncTests(ConnectingTestCase):
|
||||||
try:
|
try:
|
||||||
cnn = psycopg2.connect('dbname=thisdatabasedoesntexist', async=True)
|
cnn = psycopg2.connect('dbname=thisdatabasedoesntexist', async=True)
|
||||||
self.wait(cnn)
|
self.wait(cnn)
|
||||||
except psycopg2.Error, e:
|
except psycopg2.Error as e:
|
||||||
self.assertNotEqual(str(e), "asynchronous connection failed",
|
self.assertNotEqual(str(e), "asynchronous connection failed",
|
||||||
"connection error reason lost")
|
"connection error reason lost")
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -63,7 +63,7 @@ class CancelTests(ConnectingTestCase):
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
cur.execute("select 1")
|
cur.execute("select 1")
|
||||||
self.assertEqual(cur.fetchall(), [(1, )])
|
self.assertEqual(cur.fetchall(), [(1, )])
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
errors.append(e)
|
errors.append(e)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ class CancelTests(ConnectingTestCase):
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
try:
|
try:
|
||||||
conn.cancel()
|
conn.cancel()
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
errors.append(e)
|
errors.append(e)
|
||||||
raise
|
raise
|
||||||
del cur
|
del cur
|
||||||
|
|
|
@ -358,7 +358,7 @@ class ParseDsnTestCase(ConnectingTestCase):
|
||||||
try:
|
try:
|
||||||
# unterminated quote after dbname:
|
# unterminated quote after dbname:
|
||||||
ext.parse_dsn("dbname='test 2 user=tester password=secret")
|
ext.parse_dsn("dbname='test 2 user=tester password=secret")
|
||||||
except ProgrammingError, e:
|
except ProgrammingError as e:
|
||||||
raised = True
|
raised = True
|
||||||
self.assertTrue(str(e).find('secret') < 0,
|
self.assertTrue(str(e).find('secret') < 0,
|
||||||
"DSN was not exposed in error message")
|
"DSN was not exposed in error message")
|
||||||
|
@ -376,7 +376,7 @@ class ParseDsnTestCase(ConnectingTestCase):
|
||||||
try:
|
try:
|
||||||
# extra '=' after port value
|
# extra '=' after port value
|
||||||
ext.parse_dsn(dsn='postgresql://tester:secret@/test?port=1111=x')
|
ext.parse_dsn(dsn='postgresql://tester:secret@/test?port=1111=x')
|
||||||
except psycopg2.ProgrammingError, e:
|
except psycopg2.ProgrammingError as e:
|
||||||
raised = True
|
raised = True
|
||||||
self.assertTrue(str(e).find('secret') < 0,
|
self.assertTrue(str(e).find('secret') < 0,
|
||||||
"URI was not exposed in error message")
|
"URI was not exposed in error message")
|
||||||
|
|
|
@ -364,7 +364,7 @@ conn.close()
|
||||||
# curs.copy_from, BrokenRead(), "tcopy")
|
# curs.copy_from, BrokenRead(), "tcopy")
|
||||||
try:
|
try:
|
||||||
curs.copy_from(BrokenRead(), "tcopy")
|
curs.copy_from(BrokenRead(), "tcopy")
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
self.assert_('ZeroDivisionError' in str(e))
|
self.assert_('ZeroDivisionError' in str(e))
|
||||||
|
|
||||||
def test_copy_to_propagate_error(self):
|
def test_copy_to_propagate_error(self):
|
||||||
|
|
|
@ -48,7 +48,7 @@ class ErrocodeTests(ConnectingTestCase):
|
||||||
def f(pg_code='40001'):
|
def f(pg_code='40001'):
|
||||||
try:
|
try:
|
||||||
errorcodes.lookup(pg_code)
|
errorcodes.lookup(pg_code)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
errs.append(e)
|
errs.append(e)
|
||||||
|
|
||||||
for __ in xrange(MAX_CYCLES):
|
for __ in xrange(MAX_CYCLES):
|
||||||
|
|
|
@ -152,7 +152,7 @@ class ExceptionsTestCase(ConnectingTestCase):
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
try:
|
try:
|
||||||
cur.execute("select * from nonexist")
|
cur.execute("select * from nonexist")
|
||||||
except psycopg2.Error, exc:
|
except psycopg2.Error as exc:
|
||||||
e = exc
|
e = exc
|
||||||
|
|
||||||
self.assertEqual(e.pgcode, '42P01')
|
self.assertEqual(e.pgcode, '42P01')
|
||||||
|
@ -163,7 +163,7 @@ class ExceptionsTestCase(ConnectingTestCase):
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
try:
|
try:
|
||||||
cur.execute("select * from nonexist")
|
cur.execute("select * from nonexist")
|
||||||
except psycopg2.Error, exc:
|
except psycopg2.Error as exc:
|
||||||
e = exc
|
e = exc
|
||||||
|
|
||||||
diag = e.diag
|
diag = e.diag
|
||||||
|
@ -182,7 +182,7 @@ class ExceptionsTestCase(ConnectingTestCase):
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
try:
|
try:
|
||||||
cur.execute("select * from nonexist")
|
cur.execute("select * from nonexist")
|
||||||
except psycopg2.Error, exc:
|
except psycopg2.Error as exc:
|
||||||
e = exc
|
e = exc
|
||||||
|
|
||||||
self.assertEqual(e.diag.sqlstate, '42P01')
|
self.assertEqual(e.diag.sqlstate, '42P01')
|
||||||
|
@ -196,7 +196,7 @@ class ExceptionsTestCase(ConnectingTestCase):
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
try:
|
try:
|
||||||
cur.execute("select * from nonexist")
|
cur.execute("select * from nonexist")
|
||||||
except psycopg2.Error, exc:
|
except psycopg2.Error as exc:
|
||||||
return cur, exc
|
return cur, exc
|
||||||
|
|
||||||
cur, e = tmp()
|
cur, e = tmp()
|
||||||
|
@ -221,7 +221,7 @@ class ExceptionsTestCase(ConnectingTestCase):
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
try:
|
try:
|
||||||
cur.copy_to(f, 'nonexist')
|
cur.copy_to(f, 'nonexist')
|
||||||
except psycopg2.Error, exc:
|
except psycopg2.Error as exc:
|
||||||
diag = exc.diag
|
diag = exc.diag
|
||||||
|
|
||||||
self.assertEqual(diag.sqlstate, '42P01')
|
self.assertEqual(diag.sqlstate, '42P01')
|
||||||
|
@ -230,14 +230,14 @@ class ExceptionsTestCase(ConnectingTestCase):
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
try:
|
try:
|
||||||
cur.execute("l'acqua e' poca e 'a papera nun galleggia")
|
cur.execute("l'acqua e' poca e 'a papera nun galleggia")
|
||||||
except Exception, exc:
|
except Exception as exc:
|
||||||
diag1 = exc.diag
|
diag1 = exc.diag
|
||||||
|
|
||||||
self.conn.rollback()
|
self.conn.rollback()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cur.execute("select level from water where ducks > 1")
|
cur.execute("select level from water where ducks > 1")
|
||||||
except psycopg2.Error, exc:
|
except psycopg2.Error as exc:
|
||||||
diag2 = exc.diag
|
diag2 = exc.diag
|
||||||
|
|
||||||
self.assertEqual(diag1.sqlstate, '42601')
|
self.assertEqual(diag1.sqlstate, '42601')
|
||||||
|
@ -254,7 +254,7 @@ class ExceptionsTestCase(ConnectingTestCase):
|
||||||
cur.execute("insert into test_deferred values (1,2)")
|
cur.execute("insert into test_deferred values (1,2)")
|
||||||
try:
|
try:
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
except psycopg2.Error, exc:
|
except psycopg2.Error as exc:
|
||||||
e = exc
|
e = exc
|
||||||
self.assertEqual(e.diag.sqlstate, '23503')
|
self.assertEqual(e.diag.sqlstate, '23503')
|
||||||
|
|
||||||
|
@ -267,7 +267,7 @@ class ExceptionsTestCase(ConnectingTestCase):
|
||||||
)""")
|
)""")
|
||||||
try:
|
try:
|
||||||
cur.execute("insert into test_exc values(2)")
|
cur.execute("insert into test_exc values(2)")
|
||||||
except psycopg2.Error, exc:
|
except psycopg2.Error as exc:
|
||||||
e = exc
|
e = exc
|
||||||
self.assertEqual(e.pgcode, '23514')
|
self.assertEqual(e.pgcode, '23514')
|
||||||
self.assertEqual(e.diag.schema_name[:7], "pg_temp")
|
self.assertEqual(e.diag.schema_name[:7], "pg_temp")
|
||||||
|
@ -282,7 +282,7 @@ class ExceptionsTestCase(ConnectingTestCase):
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
try:
|
try:
|
||||||
cur.execute("select * from nonexist")
|
cur.execute("select * from nonexist")
|
||||||
except psycopg2.Error, exc:
|
except psycopg2.Error as exc:
|
||||||
e = exc
|
e = exc
|
||||||
|
|
||||||
e1 = pickle.loads(pickle.dumps(e))
|
e1 = pickle.loads(pickle.dumps(e))
|
||||||
|
@ -297,7 +297,7 @@ class ExceptionsTestCase(ConnectingTestCase):
|
||||||
import pickle
|
import pickle
|
||||||
try:
|
try:
|
||||||
psycopg2.connect('dbname=nosuchdatabasemate')
|
psycopg2.connect('dbname=nosuchdatabasemate')
|
||||||
except psycopg2.Error, exc:
|
except psycopg2.Error as exc:
|
||||||
e = exc
|
e = exc
|
||||||
|
|
||||||
e1 = pickle.loads(pickle.dumps(e))
|
e1 = pickle.loads(pickle.dumps(e))
|
||||||
|
|
|
@ -145,7 +145,7 @@ class DeadlockSerializationTests(ConnectingTestCase):
|
||||||
step1.set()
|
step1.set()
|
||||||
step2.wait()
|
step2.wait()
|
||||||
curs.execute("LOCK table2 IN ACCESS EXCLUSIVE MODE")
|
curs.execute("LOCK table2 IN ACCESS EXCLUSIVE MODE")
|
||||||
except psycopg2.DatabaseError, exc:
|
except psycopg2.DatabaseError as exc:
|
||||||
self.thread1_error = exc
|
self.thread1_error = exc
|
||||||
step1.set()
|
step1.set()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
@ -158,7 +158,7 @@ class DeadlockSerializationTests(ConnectingTestCase):
|
||||||
curs.execute("LOCK table2 IN ACCESS EXCLUSIVE MODE")
|
curs.execute("LOCK table2 IN ACCESS EXCLUSIVE MODE")
|
||||||
step2.set()
|
step2.set()
|
||||||
curs.execute("LOCK table1 IN ACCESS EXCLUSIVE MODE")
|
curs.execute("LOCK table1 IN ACCESS EXCLUSIVE MODE")
|
||||||
except psycopg2.DatabaseError, exc:
|
except psycopg2.DatabaseError as exc:
|
||||||
self.thread2_error = exc
|
self.thread2_error = exc
|
||||||
step2.set()
|
step2.set()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
@ -195,7 +195,7 @@ class DeadlockSerializationTests(ConnectingTestCase):
|
||||||
step2.wait()
|
step2.wait()
|
||||||
curs.execute("UPDATE table1 SET name='task1' WHERE id = 1")
|
curs.execute("UPDATE table1 SET name='task1' WHERE id = 1")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
except psycopg2.DatabaseError, exc:
|
except psycopg2.DatabaseError as exc:
|
||||||
self.thread1_error = exc
|
self.thread1_error = exc
|
||||||
step1.set()
|
step1.set()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
@ -207,7 +207,7 @@ class DeadlockSerializationTests(ConnectingTestCase):
|
||||||
step1.wait()
|
step1.wait()
|
||||||
curs.execute("UPDATE table1 SET name='task2' WHERE id = 1")
|
curs.execute("UPDATE table1 SET name='task2' WHERE id = 1")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
except psycopg2.DatabaseError, exc:
|
except psycopg2.DatabaseError as exc:
|
||||||
self.thread2_error = exc
|
self.thread2_error = exc
|
||||||
step2.set()
|
step2.set()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
|
@ -443,7 +443,7 @@ class ByteaParserTest(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
try:
|
try:
|
||||||
self._cast = self._import_cast()
|
self._cast = self._import_cast()
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
self._cast = None
|
self._cast = None
|
||||||
self._exc = e
|
self._exc = e
|
||||||
|
|
||||||
|
|
|
@ -113,7 +113,7 @@ class TypesExtrasTests(ConnectingTestCase):
|
||||||
psycopg2.extensions.adapt, Foo(), ext.ISQLQuote, None)
|
psycopg2.extensions.adapt, Foo(), ext.ISQLQuote, None)
|
||||||
try:
|
try:
|
||||||
psycopg2.extensions.adapt(Foo(), ext.ISQLQuote, None)
|
psycopg2.extensions.adapt(Foo(), ext.ISQLQuote, None)
|
||||||
except psycopg2.ProgrammingError, err:
|
except psycopg2.ProgrammingError as err:
|
||||||
self.failUnless(str(err) == "can't adapt type 'Foo'")
|
self.failUnless(str(err) == "can't adapt type 'Foo'")
|
||||||
|
|
||||||
def test_point_array(self):
|
def test_point_array(self):
|
||||||
|
|
|
@ -212,7 +212,7 @@ class WithCursorTestCase(WithTestCase):
|
||||||
with conn.cursor('named') as cur:
|
with conn.cursor('named') as cur:
|
||||||
cur.execute("select 1/0")
|
cur.execute("select 1/0")
|
||||||
cur.fetchone()
|
cur.fetchone()
|
||||||
except psycopg2.DataError, e:
|
except psycopg2.DataError as e:
|
||||||
self.assertEqual(e.pgcode, '22012')
|
self.assertEqual(e.pgcode, '22012')
|
||||||
else:
|
else:
|
||||||
self.fail("where is my exception?")
|
self.fail("where is my exception?")
|
||||||
|
|
|
@ -116,7 +116,7 @@ class ConnectingTestCase(unittest.TestCase):
|
||||||
def connect(self, **kwargs):
|
def connect(self, **kwargs):
|
||||||
try:
|
try:
|
||||||
self._conns
|
self._conns
|
||||||
except AttributeError, e:
|
except AttributeError as e:
|
||||||
raise AttributeError(
|
raise AttributeError(
|
||||||
"%s (did you forget to call ConnectingTestCase.setUp()?)"
|
"%s (did you forget to call ConnectingTestCase.setUp()?)"
|
||||||
% e)
|
% e)
|
||||||
|
@ -149,7 +149,7 @@ class ConnectingTestCase(unittest.TestCase):
|
||||||
conn = self.connect(**kwargs)
|
conn = self.connect(**kwargs)
|
||||||
if conn.async_ == 1:
|
if conn.async_ == 1:
|
||||||
self.wait(conn)
|
self.wait(conn)
|
||||||
except psycopg2.OperationalError, e:
|
except psycopg2.OperationalError as e:
|
||||||
# If pgcode is not set it is a genuine connection error
|
# If pgcode is not set it is a genuine connection error
|
||||||
# Otherwise we tried to run some bad operation in the connection
|
# Otherwise we tried to run some bad operation in the connection
|
||||||
# (e.g. bug #482) and we'd rather know that.
|
# (e.g. bug #482) and we'd rather know that.
|
||||||
|
@ -388,7 +388,7 @@ def skip_if_no_superuser(f):
|
||||||
from psycopg2 import ProgrammingError
|
from psycopg2 import ProgrammingError
|
||||||
try:
|
try:
|
||||||
return f(self)
|
return f(self)
|
||||||
except ProgrammingError, e:
|
except ProgrammingError as e:
|
||||||
import psycopg2.errorcodes
|
import psycopg2.errorcodes
|
||||||
if e.pgcode == psycopg2.errorcodes.INSUFFICIENT_PRIVILEGE:
|
if e.pgcode == psycopg2.errorcodes.INSUFFICIENT_PRIVILEGE:
|
||||||
self.skipTest("skipped because not superuser")
|
self.skipTest("skipped because not superuser")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user