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:
parent
2f3c233f38
commit
b69457ccdf
|
@ -59,7 +59,7 @@ between 8.1 and 10 are included in the module.
|
||||||
|
|
||||||
>>> try:
|
>>> try:
|
||||||
... cur.execute("SELECT ouch FROM aargh;")
|
... cur.execute("SELECT ouch FROM aargh;")
|
||||||
... except Exception, e:
|
... except Exception as e:
|
||||||
... pass
|
... pass
|
||||||
...
|
...
|
||||||
>>> errorcodes.lookup(e.pgcode[:2])
|
>>> errorcodes.lookup(e.pgcode[:2])
|
||||||
|
|
|
@ -27,9 +27,9 @@ import psycopg2
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dsn:", DSN
|
print("Opening connection using dsn:", DSN)
|
||||||
conn = psycopg2.connect(DSN)
|
conn = psycopg2.connect(DSN)
|
||||||
print "Encoding for this connection is", conn.encoding
|
print("Encoding for this connection is", conn.encoding)
|
||||||
|
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
try:
|
try:
|
||||||
|
@ -51,16 +51,16 @@ io.close()
|
||||||
|
|
||||||
io = open('copy_from.txt', 'r')
|
io = open('copy_from.txt', 'r')
|
||||||
curs.copy_from(io, 'test_copy')
|
curs.copy_from(io, 'test_copy')
|
||||||
print "1) Copy %d records from file object " % len(data) + \
|
print("1) Copy %d records from file object " % len(data) +
|
||||||
"using defaults (sep: \\t and null = \\N)"
|
"using defaults (sep: \\t and null = \\N)")
|
||||||
io.close()
|
io.close()
|
||||||
|
|
||||||
curs.execute("SELECT * FROM test_copy")
|
curs.execute("SELECT * FROM test_copy")
|
||||||
rows = curs.fetchall()
|
rows = curs.fetchall()
|
||||||
print " Select returned %d rows" % len(rows)
|
print(" Select returned %d rows" % len(rows))
|
||||||
|
|
||||||
for r in 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")
|
curs.execute("delete from test_copy")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
@ -75,15 +75,15 @@ io.close()
|
||||||
|
|
||||||
io = open('copy_from.txt', 'r')
|
io = open('copy_from.txt', 'r')
|
||||||
curs.copy_from(io, 'test_copy', ':')
|
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()
|
io.close()
|
||||||
|
|
||||||
curs.execute("SELECT * FROM test_copy")
|
curs.execute("SELECT * FROM test_copy")
|
||||||
rows = curs.fetchall()
|
rows = curs.fetchall()
|
||||||
print " Select returned %d rows" % len(rows)
|
print(" Select returned %d rows" % len(rows))
|
||||||
|
|
||||||
for r in 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")
|
curs.execute("delete from test_copy")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
@ -98,15 +98,15 @@ io.close()
|
||||||
|
|
||||||
io = open('copy_from.txt', 'r')
|
io = open('copy_from.txt', 'r')
|
||||||
curs.copy_from(io, 'test_copy', null='NULL')
|
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()
|
io.close()
|
||||||
|
|
||||||
curs.execute("SELECT * FROM test_copy")
|
curs.execute("SELECT * FROM test_copy")
|
||||||
rows = curs.fetchall()
|
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:
|
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")
|
curs.execute("delete from test_copy")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
@ -119,16 +119,16 @@ io.close()
|
||||||
|
|
||||||
io = open('copy_from.txt', 'r')
|
io = open('copy_from.txt', 'r')
|
||||||
curs.copy_from(io, 'test_copy', ':', 'NULL')
|
curs.copy_from(io, 'test_copy', ':', 'NULL')
|
||||||
print "4) Copy %d records from file object " % len(data) + \
|
print("4) Copy %d records from file object " % len(data) +
|
||||||
"using sep = : and null = NULL"
|
"using sep = : and null = NULL")
|
||||||
io.close()
|
io.close()
|
||||||
|
|
||||||
curs.execute("SELECT * FROM test_copy")
|
curs.execute("SELECT * FROM test_copy")
|
||||||
rows = curs.fetchall()
|
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:
|
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")
|
curs.execute("delete from test_copy")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
@ -141,20 +141,20 @@ data.write('\n'.join(['Tom\tJenkins\t37',
|
||||||
data.seek(0)
|
data.seek(0)
|
||||||
|
|
||||||
curs.copy_from(data, 'test_copy')
|
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")
|
curs.execute("SELECT * FROM test_copy")
|
||||||
rows = curs.fetchall()
|
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:
|
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")
|
curs.execute("delete from test_copy")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
# simple error test
|
# simple error test
|
||||||
|
|
||||||
print "6) About to raise an error"
|
print("6) About to raise an error")
|
||||||
data = StringIO.StringIO()
|
data = StringIO.StringIO()
|
||||||
data.write('\n'.join(['Tom\tJenkins\t37',
|
data.write('\n'.join(['Tom\tJenkins\t37',
|
||||||
'Madonna\t\N\t45',
|
'Madonna\t\N\t45',
|
||||||
|
@ -163,9 +163,9 @@ data.seek(0)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
curs.copy_from(data, 'test_copy')
|
curs.copy_from(data, 'test_copy')
|
||||||
except StandardError, err:
|
except StandardError as err:
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
print " Caught error (as expected):\n", err
|
print(" Caught error (as expected):\n", err)
|
||||||
|
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
|
|
||||||
|
|
|
@ -25,9 +25,9 @@ import psycopg2.extensions
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
DSN = sys.argv[1]
|
DSN = sys.argv[1]
|
||||||
|
|
||||||
print "Opening connection using dsn:", DSN
|
print("Opening connection using dsn:", DSN)
|
||||||
conn = psycopg2.connect(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):
|
class NoDataError(psycopg2.ProgrammingError):
|
||||||
|
@ -52,12 +52,12 @@ class Cursor(psycopg2.extensions.cursor):
|
||||||
|
|
||||||
curs = conn.cursor(cursor_factory=Cursor)
|
curs = conn.cursor(cursor_factory=Cursor)
|
||||||
curs.execute("SELECT 1 AS foo")
|
curs.execute("SELECT 1 AS foo")
|
||||||
print "Result of fetchone():", curs.fetchone()
|
print("Result of fetchone():", curs.fetchone())
|
||||||
|
|
||||||
# now let's raise the exception
|
# now let's raise the exception
|
||||||
try:
|
try:
|
||||||
curs.fetchone()
|
curs.fetchone()
|
||||||
except NoDataError, err:
|
except NoDataError as err:
|
||||||
print "Exception caught:", err
|
print("Exception caught:", err)
|
||||||
|
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
|
|
|
@ -84,7 +84,7 @@ def insert_func(conn_or_pool, rows):
|
||||||
try:
|
try:
|
||||||
c.execute("INSERT INTO test_threads VALUES (%s, %s, %s)",
|
c.execute("INSERT INTO test_threads VALUES (%s, %s, %s)",
|
||||||
(str(i), i, float(i)))
|
(str(i), i, float(i)))
|
||||||
except psycopg2.ProgrammingError, err:
|
except psycopg2.ProgrammingError as err:
|
||||||
print name, ": an error occurred; skipping this insert"
|
print name, ": an error occurred; skipping this insert"
|
||||||
print err
|
print err
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
@ -112,10 +112,10 @@ def select_func(conn_or_pool, z):
|
||||||
if MODE == 1:
|
if MODE == 1:
|
||||||
conn_or_pool.putconn(conn)
|
conn_or_pool.putconn(conn)
|
||||||
s = name + ": number of rows fetched: " + str(len(l))
|
s = name + ": number of rows fetched: " + str(len(l))
|
||||||
print s
|
print(s)
|
||||||
except psycopg2.ProgrammingError, err:
|
except psycopg2.ProgrammingError as err:
|
||||||
print name, ": an error occurred; skipping this select"
|
print(name, ": an error occurred; skipping this select")
|
||||||
print err
|
print(err)
|
||||||
|
|
||||||
## create the connection pool or the connections
|
## create the connection pool or the connections
|
||||||
if MODE == 0:
|
if MODE == 0:
|
||||||
|
@ -129,14 +129,14 @@ else:
|
||||||
## create the threads
|
## create the threads
|
||||||
threads = []
|
threads = []
|
||||||
|
|
||||||
print "Creating INSERT threads:"
|
print("Creating INSERT threads:")
|
||||||
for name in INSERT_THREADS:
|
for name in INSERT_THREADS:
|
||||||
t = threading.Thread(None, insert_func, 'Thread-'+name,
|
t = threading.Thread(None, insert_func, 'Thread-'+name,
|
||||||
(conn_insert, ROWS))
|
(conn_insert, ROWS))
|
||||||
t.setDaemon(0)
|
t.setDaemon(0)
|
||||||
threads.append(t)
|
threads.append(t)
|
||||||
|
|
||||||
print "Creating SELECT threads:"
|
print("Creating SELECT threads:")
|
||||||
for name in SELECT_THREADS:
|
for name in SELECT_THREADS:
|
||||||
t = threading.Thread(None, select_func, 'Thread-'+name,
|
t = threading.Thread(None, select_func, 'Thread-'+name,
|
||||||
(conn_select, SELECT_DIV))
|
(conn_select, SELECT_DIV))
|
||||||
|
@ -150,12 +150,12 @@ for t in threads:
|
||||||
# and wait for them to finish
|
# and wait for them to finish
|
||||||
for t in threads:
|
for t in threads:
|
||||||
t.join()
|
t.join()
|
||||||
print t.getName(), "exited OK"
|
print(t.getName(), "exited OK")
|
||||||
|
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
curs.execute("SELECT count(name) FROM test_threads")
|
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")
|
curs.execute("DROP TABLE test_threads")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user