1
1
mirror of https://github.com/psycopg/psycopg2.git synced 2025-02-26 13:40:33 +03:00

Update to Exception as e, print()

This commit is contained in:
Hugo 2017-11-28 10:30:15 +02:00 committed by Daniele Varrazzo
parent 2f3c233f38
commit b69457ccdf
4 changed files with 37 additions and 37 deletions

View File

@ -59,7 +59,7 @@ between 8.1 and 10 are included in the module.
>>> try:
... cur.execute("SELECT ouch FROM aargh;")
... except Exception, e:
... except Exception as e:
... pass
...
>>> errorcodes.lookup(e.pgcode[:2])

View File

@ -27,9 +27,9 @@ import psycopg2
if len(sys.argv) > 1:
DSN = sys.argv[1]
print "Opening connection using dsn:", DSN
print("Opening connection using dsn:", DSN)
conn = psycopg2.connect(DSN)
print "Encoding for this connection is", conn.encoding
print("Encoding for this connection is", conn.encoding)
curs = conn.cursor()
try:
@ -51,16 +51,16 @@ io.close()
io = open('copy_from.txt', 'r')
curs.copy_from(io, 'test_copy')
print "1) Copy %d records from file object " % len(data) + \
"using defaults (sep: \\t and null = \\N)"
print("1) Copy %d records from file object " % len(data) +
"using defaults (sep: \\t and null = \\N)")
io.close()
curs.execute("SELECT * FROM test_copy")
rows = curs.fetchall()
print " Select returned %d rows" % len(rows)
print(" Select returned %d rows" % len(rows))
for r in rows:
print " %s %s\t%s" % (r[0], r[1], r[2])
print(" %s %s\t%s" % (r[0], r[1], r[2]))
curs.execute("delete from test_copy")
conn.commit()
@ -75,15 +75,15 @@ io.close()
io = open('copy_from.txt', 'r')
curs.copy_from(io, 'test_copy', ':')
print "2) Copy %d records from file object using sep = :" % len(data)
print("2) Copy %d records from file object using sep = :" % len(data))
io.close()
curs.execute("SELECT * FROM test_copy")
rows = curs.fetchall()
print " Select returned %d rows" % len(rows)
print(" Select returned %d rows" % len(rows))
for r in rows:
print " %s %s\t%s" % (r[0], r[1], r[2])
print(" %s %s\t%s" % (r[0], r[1], r[2]))
curs.execute("delete from test_copy")
conn.commit()
@ -98,15 +98,15 @@ io.close()
io = open('copy_from.txt', 'r')
curs.copy_from(io, 'test_copy', null='NULL')
print "3) Copy %d records from file object using null = NULL" % len(data)
print("3) Copy %d records from file object using null = NULL" % len(data))
io.close()
curs.execute("SELECT * FROM test_copy")
rows = curs.fetchall()
print " Select using cursor returned %d rows" % len(rows)
print(" Select using cursor returned %d rows" % len(rows))
for r in rows:
print " %s %s\t%s" % (r[0], r[1], r[2])
print(" %s %s\t%s" % (r[0], r[1], r[2]))
curs.execute("delete from test_copy")
conn.commit()
@ -119,16 +119,16 @@ io.close()
io = open('copy_from.txt', 'r')
curs.copy_from(io, 'test_copy', ':', 'NULL')
print "4) Copy %d records from file object " % len(data) + \
"using sep = : and null = NULL"
print("4) Copy %d records from file object " % len(data) +
"using sep = : and null = NULL")
io.close()
curs.execute("SELECT * FROM test_copy")
rows = curs.fetchall()
print " Select using cursor returned %d rows" % len(rows)
print(" Select using cursor returned %d rows" % len(rows))
for r in rows:
print " %s %s\t%s" % (r[0], r[1], r[2])
print(" %s %s\t%s" % (r[0], r[1], r[2]))
curs.execute("delete from test_copy")
conn.commit()
@ -141,20 +141,20 @@ data.write('\n'.join(['Tom\tJenkins\t37',
data.seek(0)
curs.copy_from(data, 'test_copy')
print "5) Copy 3 records from StringIO object using defaults"
print("5) Copy 3 records from StringIO object using defaults")
curs.execute("SELECT * FROM test_copy")
rows = curs.fetchall()
print " Select using cursor returned %d rows" % len(rows)
print(" Select using cursor returned %d rows" % len(rows))
for r in rows:
print " %s %s\t%s" % (r[0], r[1], r[2])
print(" %s %s\t%s" % (r[0], r[1], r[2]))
curs.execute("delete from test_copy")
conn.commit()
# simple error test
print "6) About to raise an error"
print("6) About to raise an error")
data = StringIO.StringIO()
data.write('\n'.join(['Tom\tJenkins\t37',
'Madonna\t\N\t45',
@ -163,9 +163,9 @@ data.seek(0)
try:
curs.copy_from(data, 'test_copy')
except StandardError, err:
except StandardError as err:
conn.rollback()
print " Caught error (as expected):\n", err
print(" Caught error (as expected):\n", err)
conn.rollback()

View File

@ -25,9 +25,9 @@ import psycopg2.extensions
if len(sys.argv) > 1:
DSN = sys.argv[1]
print "Opening connection using dsn:", DSN
print("Opening connection using dsn:", DSN)
conn = psycopg2.connect(DSN)
print "Encoding for this connection is", conn.encoding
print("Encoding for this connection is", conn.encoding)
class NoDataError(psycopg2.ProgrammingError):
@ -52,12 +52,12 @@ class Cursor(psycopg2.extensions.cursor):
curs = conn.cursor(cursor_factory=Cursor)
curs.execute("SELECT 1 AS foo")
print "Result of fetchone():", curs.fetchone()
print("Result of fetchone():", curs.fetchone())
# now let's raise the exception
try:
curs.fetchone()
except NoDataError, err:
print "Exception caught:", err
except NoDataError as err:
print("Exception caught:", err)
conn.rollback()

View File

@ -84,7 +84,7 @@ def insert_func(conn_or_pool, rows):
try:
c.execute("INSERT INTO test_threads VALUES (%s, %s, %s)",
(str(i), i, float(i)))
except psycopg2.ProgrammingError, err:
except psycopg2.ProgrammingError as err:
print name, ": an error occurred; skipping this insert"
print err
conn.commit()
@ -112,10 +112,10 @@ def select_func(conn_or_pool, z):
if MODE == 1:
conn_or_pool.putconn(conn)
s = name + ": number of rows fetched: " + str(len(l))
print s
except psycopg2.ProgrammingError, err:
print name, ": an error occurred; skipping this select"
print err
print(s)
except psycopg2.ProgrammingError as err:
print(name, ": an error occurred; skipping this select")
print(err)
## create the connection pool or the connections
if MODE == 0:
@ -129,14 +129,14 @@ else:
## create the threads
threads = []
print "Creating INSERT threads:"
print("Creating INSERT threads:")
for name in INSERT_THREADS:
t = threading.Thread(None, insert_func, 'Thread-'+name,
(conn_insert, ROWS))
t.setDaemon(0)
threads.append(t)
print "Creating SELECT threads:"
print("Creating SELECT threads:")
for name in SELECT_THREADS:
t = threading.Thread(None, select_func, 'Thread-'+name,
(conn_select, SELECT_DIV))
@ -150,12 +150,12 @@ for t in threads:
# and wait for them to finish
for t in threads:
t.join()
print t.getName(), "exited OK"
print(t.getName(), "exited OK")
conn.commit()
curs.execute("SELECT count(name) FROM test_threads")
print "Inserted", curs.fetchone()[0], "rows."
print("Inserted", curs.fetchone()[0], "rows.")
curs.execute("DROP TABLE test_threads")
conn.commit()