Commit 45223fd9 authored by Peter Eisentraut's avatar Peter Eisentraut

Modernize Python exception syntax in tests

Change the exception syntax used in the tests to use the more current

    except Exception as ex:

rather than the old

    except Exception, ex:

Since support for Python <2.6 has been removed, all supported versions
now support the new style, and we can save one step in the Python 3
compatibility conversion.
Reviewed-by: default avatarTom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/98b69261-298c-13d2-f34d-836fd9c29b21%402ndquadrant.com
parent 37f21ed1
...@@ -186,7 +186,7 @@ DETAIL: message:(plpy.Error: message text), detail:(detail text), hint: (hint t ...@@ -186,7 +186,7 @@ DETAIL: message:(plpy.Error: message text), detail:(detail text), hint: (hint t
DO $$ DO $$
try: try:
plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')") plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')")
except Exception, e: except Exception as e:
plpy.info(e.spidata) plpy.info(e.spidata)
raise e raise e
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
...@@ -196,7 +196,7 @@ HINT: some hint ...@@ -196,7 +196,7 @@ HINT: some hint
DO $$ DO $$
try: try:
plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type') plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type')
except Exception, e: except Exception as e:
plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name)) plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
raise e raise e
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
......
...@@ -97,7 +97,7 @@ CREATE FUNCTION invalid_type_caught(a text) RETURNS text ...@@ -97,7 +97,7 @@ CREATE FUNCTION invalid_type_caught(a text) RETURNS text
q = "SELECT fname FROM users WHERE lname = $1" q = "SELECT fname FROM users WHERE lname = $1"
try: try:
SD["plan"] = plpy.prepare(q, [ "test" ]) SD["plan"] = plpy.prepare(q, [ "test" ])
except plpy.SPIError, ex: except plpy.SPIError as ex:
plpy.notice(str(ex)) plpy.notice(str(ex))
return None return None
rv = plpy.execute(SD["plan"], [ a ]) rv = plpy.execute(SD["plan"], [ a ])
...@@ -122,7 +122,7 @@ CREATE FUNCTION invalid_type_reraised(a text) RETURNS text ...@@ -122,7 +122,7 @@ CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
q = "SELECT fname FROM users WHERE lname = $1" q = "SELECT fname FROM users WHERE lname = $1"
try: try:
SD["plan"] = plpy.prepare(q, [ "test" ]) SD["plan"] = plpy.prepare(q, [ "test" ])
except plpy.SPIError, ex: except plpy.SPIError as ex:
plpy.error(str(ex)) plpy.error(str(ex))
rv = plpy.execute(SD["plan"], [ a ]) rv = plpy.execute(SD["plan"], [ a ])
if len(rv): if len(rv):
...@@ -321,9 +321,9 @@ $$ ...@@ -321,9 +321,9 @@ $$
from plpy import spiexceptions from plpy import spiexceptions
try: try:
plpy.execute("insert into specific values (%s)" % (i or "NULL")); plpy.execute("insert into specific values (%s)" % (i or "NULL"));
except spiexceptions.NotNullViolation, e: except spiexceptions.NotNullViolation as e:
plpy.notice("Violated the NOT NULL constraint, sqlstate %s" % e.sqlstate) plpy.notice("Violated the NOT NULL constraint, sqlstate %s" % e.sqlstate)
except spiexceptions.UniqueViolation, e: except spiexceptions.UniqueViolation as e:
plpy.notice("Violated the UNIQUE constraint, sqlstate %s" % e.sqlstate) plpy.notice("Violated the UNIQUE constraint, sqlstate %s" % e.sqlstate)
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
SELECT specific_exception(2); SELECT specific_exception(2);
......
...@@ -21,7 +21,7 @@ CREATE FUNCTION import_succeed() returns text ...@@ -21,7 +21,7 @@ CREATE FUNCTION import_succeed() returns text
import re import re
import string import string
import time import time
except Exception, ex: except Exception as ex:
plpy.notice("import failed -- %s" % str(ex)) plpy.notice("import failed -- %s" % str(ex))
return "failed, that wasn''t supposed to happen" return "failed, that wasn''t supposed to happen"
return "succeeded, as expected"' return "succeeded, as expected"'
......
...@@ -25,7 +25,7 @@ CREATE FUNCTION test_param_names3(a0 integer) RETURNS boolean AS $$ ...@@ -25,7 +25,7 @@ CREATE FUNCTION test_param_names3(a0 integer) RETURNS boolean AS $$
try: try:
assert a1 == args[0] assert a1 == args[0]
return False return False
except NameError, e: except NameError as e:
assert e.args[0].find("a1") > -1 assert e.args[0].find("a1") > -1
return True return True
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
......
...@@ -26,7 +26,7 @@ CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text ...@@ -26,7 +26,7 @@ CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text
try: try:
rv = plpy.execute(SD["myplan"], [a]) rv = plpy.execute(SD["myplan"], [a])
return "there are " + str(rv[0]["count"]) + " " + str(a) + "s" return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
except Exception, ex: except Exception as ex:
plpy.error(str(ex)) plpy.error(str(ex))
return None return None
' '
...@@ -39,7 +39,7 @@ CREATE FUNCTION spi_prepared_plan_test_two(a text) RETURNS text ...@@ -39,7 +39,7 @@ CREATE FUNCTION spi_prepared_plan_test_two(a text) RETURNS text
try: try:
rv = SD["myplan"].execute([a]) rv = SD["myplan"].execute([a])
return "there are " + str(rv[0]["count"]) + " " + str(a) + "s" return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
except Exception, ex: except Exception as ex:
plpy.error(str(ex)) plpy.error(str(ex))
return None return None
' '
...@@ -53,7 +53,7 @@ try: ...@@ -53,7 +53,7 @@ try:
rv = plpy.execute(SD["myplan"]) rv = plpy.execute(SD["myplan"])
if len(rv): if len(rv):
return rv[0]["count"] return rv[0]["count"]
except Exception, ex: except Exception as ex:
plpy.error(str(ex)) plpy.error(str(ex))
return None return None
' '
......
...@@ -66,7 +66,7 @@ with plpy.subtransaction(): ...@@ -66,7 +66,7 @@ with plpy.subtransaction():
with plpy.subtransaction(): with plpy.subtransaction():
plpy.execute("INSERT INTO subtransaction_tbl VALUES (3)") plpy.execute("INSERT INTO subtransaction_tbl VALUES (3)")
plpy.execute("error") plpy.execute("error")
except plpy.SPIError, e: except plpy.SPIError as e:
if not swallow: if not swallow:
raise raise
plpy.notice("Swallowed %s(%r)" % (e.__class__.__name__, e.args[0])) plpy.notice("Swallowed %s(%r)" % (e.__class__.__name__, e.args[0]))
......
...@@ -400,7 +400,7 @@ CREATE FUNCTION test_type_unmarshal(x bytea) RETURNS text AS $$ ...@@ -400,7 +400,7 @@ CREATE FUNCTION test_type_unmarshal(x bytea) RETURNS text AS $$
import marshal import marshal
try: try:
return marshal.loads(x) return marshal.loads(x)
except ValueError, e: except ValueError as e:
return 'FAILED: ' + str(e) return 'FAILED: ' + str(e)
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
SELECT test_type_unmarshal(x) FROM test_type_marshal() x; SELECT test_type_unmarshal(x) FROM test_type_marshal() x;
......
...@@ -14,7 +14,7 @@ REGRESS := $(foreach test,$(REGRESS),$(if $(filter $(test),$(REGRESS_PLPYTHON3_M ...@@ -14,7 +14,7 @@ REGRESS := $(foreach test,$(REGRESS),$(if $(filter $(test),$(REGRESS_PLPYTHON3_M
pgregress-python3-mangle: pgregress-python3-mangle:
$(MKDIR_P) sql/python3 expected/python3 results/python3 $(MKDIR_P) sql/python3 expected/python3 results/python3
for file in $(patsubst %,$(srcdir)/sql/%.sql,$(REGRESS_PLPYTHON3_MANGLE)) $(patsubst %,$(srcdir)/expected/%*.out,$(REGRESS_PLPYTHON3_MANGLE)); do \ for file in $(patsubst %,$(srcdir)/sql/%.sql,$(REGRESS_PLPYTHON3_MANGLE)) $(patsubst %,$(srcdir)/expected/%*.out,$(REGRESS_PLPYTHON3_MANGLE)); do \
sed -e 's/except \([[:alpha:]][[:alpha:].]*\), *\([[:alpha:]][[:alpha:]]*\):/except \1 as \2:/g' \ sed \
-e "s/<type 'exceptions\.\([[:alpha:]]*\)'>/<class '\1'>/g" \ -e "s/<type 'exceptions\.\([[:alpha:]]*\)'>/<class '\1'>/g" \
-e "s/<type 'long'>/<class 'int'>/g" \ -e "s/<type 'long'>/<class 'int'>/g" \
-e "s/\([0-9][0-9]*\)L/\1/g" \ -e "s/\([0-9][0-9]*\)L/\1/g" \
......
...@@ -125,7 +125,7 @@ $$; ...@@ -125,7 +125,7 @@ $$;
DO $$ DO $$
try: try:
plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')") plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')")
except Exception, e: except Exception as e:
plpy.info(e.spidata) plpy.info(e.spidata)
raise e raise e
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
...@@ -133,7 +133,7 @@ $$ LANGUAGE plpythonu; ...@@ -133,7 +133,7 @@ $$ LANGUAGE plpythonu;
DO $$ DO $$
try: try:
plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type') plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type')
except Exception, e: except Exception as e:
plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name)) plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
raise e raise e
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
...@@ -82,7 +82,7 @@ CREATE FUNCTION invalid_type_caught(a text) RETURNS text ...@@ -82,7 +82,7 @@ CREATE FUNCTION invalid_type_caught(a text) RETURNS text
q = "SELECT fname FROM users WHERE lname = $1" q = "SELECT fname FROM users WHERE lname = $1"
try: try:
SD["plan"] = plpy.prepare(q, [ "test" ]) SD["plan"] = plpy.prepare(q, [ "test" ])
except plpy.SPIError, ex: except plpy.SPIError as ex:
plpy.notice(str(ex)) plpy.notice(str(ex))
return None return None
rv = plpy.execute(SD["plan"], [ a ]) rv = plpy.execute(SD["plan"], [ a ])
...@@ -104,7 +104,7 @@ CREATE FUNCTION invalid_type_reraised(a text) RETURNS text ...@@ -104,7 +104,7 @@ CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
q = "SELECT fname FROM users WHERE lname = $1" q = "SELECT fname FROM users WHERE lname = $1"
try: try:
SD["plan"] = plpy.prepare(q, [ "test" ]) SD["plan"] = plpy.prepare(q, [ "test" ])
except plpy.SPIError, ex: except plpy.SPIError as ex:
plpy.error(str(ex)) plpy.error(str(ex))
rv = plpy.execute(SD["plan"], [ a ]) rv = plpy.execute(SD["plan"], [ a ])
if len(rv): if len(rv):
...@@ -247,9 +247,9 @@ $$ ...@@ -247,9 +247,9 @@ $$
from plpy import spiexceptions from plpy import spiexceptions
try: try:
plpy.execute("insert into specific values (%s)" % (i or "NULL")); plpy.execute("insert into specific values (%s)" % (i or "NULL"));
except spiexceptions.NotNullViolation, e: except spiexceptions.NotNullViolation as e:
plpy.notice("Violated the NOT NULL constraint, sqlstate %s" % e.sqlstate) plpy.notice("Violated the NOT NULL constraint, sqlstate %s" % e.sqlstate)
except spiexceptions.UniqueViolation, e: except spiexceptions.UniqueViolation as e:
plpy.notice("Violated the UNIQUE constraint, sqlstate %s" % e.sqlstate) plpy.notice("Violated the UNIQUE constraint, sqlstate %s" % e.sqlstate)
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
......
...@@ -24,7 +24,7 @@ CREATE FUNCTION import_succeed() returns text ...@@ -24,7 +24,7 @@ CREATE FUNCTION import_succeed() returns text
import re import re
import string import string
import time import time
except Exception, ex: except Exception as ex:
plpy.notice("import failed -- %s" % str(ex)) plpy.notice("import failed -- %s" % str(ex))
return "failed, that wasn''t supposed to happen" return "failed, that wasn''t supposed to happen"
return "succeeded, as expected"' return "succeeded, as expected"'
......
...@@ -29,7 +29,7 @@ CREATE FUNCTION test_param_names3(a0 integer) RETURNS boolean AS $$ ...@@ -29,7 +29,7 @@ CREATE FUNCTION test_param_names3(a0 integer) RETURNS boolean AS $$
try: try:
assert a1 == args[0] assert a1 == args[0]
return False return False
except NameError, e: except NameError as e:
assert e.args[0].find("a1") > -1 assert e.args[0].find("a1") > -1
return True return True
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
......
...@@ -31,7 +31,7 @@ CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text ...@@ -31,7 +31,7 @@ CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text
try: try:
rv = plpy.execute(SD["myplan"], [a]) rv = plpy.execute(SD["myplan"], [a])
return "there are " + str(rv[0]["count"]) + " " + str(a) + "s" return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
except Exception, ex: except Exception as ex:
plpy.error(str(ex)) plpy.error(str(ex))
return None return None
' '
...@@ -45,7 +45,7 @@ CREATE FUNCTION spi_prepared_plan_test_two(a text) RETURNS text ...@@ -45,7 +45,7 @@ CREATE FUNCTION spi_prepared_plan_test_two(a text) RETURNS text
try: try:
rv = SD["myplan"].execute([a]) rv = SD["myplan"].execute([a])
return "there are " + str(rv[0]["count"]) + " " + str(a) + "s" return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
except Exception, ex: except Exception as ex:
plpy.error(str(ex)) plpy.error(str(ex))
return None return None
' '
...@@ -60,7 +60,7 @@ try: ...@@ -60,7 +60,7 @@ try:
rv = plpy.execute(SD["myplan"]) rv = plpy.execute(SD["myplan"])
if len(rv): if len(rv):
return rv[0]["count"] return rv[0]["count"]
except Exception, ex: except Exception as ex:
plpy.error(str(ex)) plpy.error(str(ex))
return None return None
' '
......
...@@ -40,7 +40,7 @@ with plpy.subtransaction(): ...@@ -40,7 +40,7 @@ with plpy.subtransaction():
with plpy.subtransaction(): with plpy.subtransaction():
plpy.execute("INSERT INTO subtransaction_tbl VALUES (3)") plpy.execute("INSERT INTO subtransaction_tbl VALUES (3)")
plpy.execute("error") plpy.execute("error")
except plpy.SPIError, e: except plpy.SPIError as e:
if not swallow: if not swallow:
raise raise
plpy.notice("Swallowed %s(%r)" % (e.__class__.__name__, e.args[0])) plpy.notice("Swallowed %s(%r)" % (e.__class__.__name__, e.args[0]))
......
...@@ -163,7 +163,7 @@ CREATE FUNCTION test_type_unmarshal(x bytea) RETURNS text AS $$ ...@@ -163,7 +163,7 @@ CREATE FUNCTION test_type_unmarshal(x bytea) RETURNS text AS $$
import marshal import marshal
try: try:
return marshal.loads(x) return marshal.loads(x)
except ValueError, e: except ValueError as e:
return 'FAILED: ' + str(e) return 'FAILED: ' + str(e)
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
......
...@@ -290,7 +290,6 @@ sub mangle_plpython3 ...@@ -290,7 +290,6 @@ sub mangle_plpython3
close($handle); close($handle);
do do
{ {
s/except ([[:alpha:]][[:alpha:].]*), *([[:alpha:]][[:alpha:]]*):/except $1 as $2:/g;
s/<type 'exceptions\.([[:alpha:]]*)'>/<class '$1'>/g; s/<type 'exceptions\.([[:alpha:]]*)'>/<class '$1'>/g;
s/<type 'long'>/<class 'int'>/g; s/<type 'long'>/<class 'int'>/g;
s/([0-9][0-9]*)L/$1/g; s/([0-9][0-9]*)L/$1/g;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment