Modernize Python exception syntax in tests
authorPeter Eisentraut <peter@eisentraut.org>
Wed, 8 Jan 2020 20:48:44 +0000 (21:48 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Wed, 8 Jan 2020 21:47:22 +0000 (22:47 +0100)
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: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://www.postgresql.org/message-id/flat/98b69261-298c-13d2-f34d-836fd9c29b21%402ndquadrant.com

16 files changed:
src/pl/plpython/expected/plpython_ereport.out
src/pl/plpython/expected/plpython_error.out
src/pl/plpython/expected/plpython_import.out
src/pl/plpython/expected/plpython_params.out
src/pl/plpython/expected/plpython_spi.out
src/pl/plpython/expected/plpython_subtransaction.out
src/pl/plpython/expected/plpython_types.out
src/pl/plpython/regress-python3-mangle.mk
src/pl/plpython/sql/plpython_ereport.sql
src/pl/plpython/sql/plpython_error.sql
src/pl/plpython/sql/plpython_import.sql
src/pl/plpython/sql/plpython_params.sql
src/pl/plpython/sql/plpython_spi.sql
src/pl/plpython/sql/plpython_subtransaction.sql
src/pl/plpython/sql/plpython_types.sql
src/tools/msvc/vcregress.pl

index e11999ce8c8c666300252c521510002e8648345c..b73bfff51154aa3dffc5deab07853f013525bb64 100644 (file)
@@ -186,7 +186,7 @@ DETAIL:  message:(plpy.Error: message text), detail:(detail text), hint: (hint t
 DO $$
 try:
     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)
     raise e
 $$ LANGUAGE plpythonu;
@@ -196,7 +196,7 @@ HINT:  some hint
 DO $$
 try:
     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))
     raise e
 $$ LANGUAGE plpythonu;
index 4d615b41cc50960394cef51890b032044f3defef..b2f8fe83eb60f61b1999a9b9aa17c6ada19c26da 100644 (file)
@@ -97,7 +97,7 @@ CREATE FUNCTION invalid_type_caught(a text) RETURNS text
    q = "SELECT fname FROM users WHERE lname = $1"
    try:
        SD["plan"] = plpy.prepare(q, [ "test" ])
-   except plpy.SPIError, ex:
+   except plpy.SPIError as ex:
        plpy.notice(str(ex))
        return None
 rv = plpy.execute(SD["plan"], [ a ])
@@ -122,7 +122,7 @@ CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
    q = "SELECT fname FROM users WHERE lname = $1"
    try:
        SD["plan"] = plpy.prepare(q, [ "test" ])
-   except plpy.SPIError, ex:
+   except plpy.SPIError as ex:
        plpy.error(str(ex))
 rv = plpy.execute(SD["plan"], [ a ])
 if len(rv):
@@ -321,9 +321,9 @@ $$
 from plpy import spiexceptions
 try:
     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)
-except spiexceptions.UniqueViolation, e:
+except spiexceptions.UniqueViolation as e:
     plpy.notice("Violated the UNIQUE constraint, sqlstate %s" % e.sqlstate)
 $$ LANGUAGE plpythonu;
 SELECT specific_exception(2);
index 1d981eacf127e258631f128ecf5a435740c8edb7..b59e1821a79490fcb4f1c5966412b1e3ca025d17 100644 (file)
@@ -21,7 +21,7 @@ CREATE FUNCTION import_succeed() returns text
   import re
   import string
   import time
-except Exception, ex:
+except Exception as ex:
    plpy.notice("import failed -- %s" % str(ex))
    return "failed, that wasn''t supposed to happen"
 return "succeeded, as expected"'
index 8dc3802513030950a64b63c6d2efd1e8a09bd59d..46ea7dfb90b7b72876b831eaa5d9e6422d981d83 100644 (file)
@@ -25,7 +25,7 @@ CREATE FUNCTION test_param_names3(a0 integer) RETURNS boolean AS $$
 try:
    assert a1 == args[0]
    return False
-except NameError, e:
+except NameError as e:
    assert e.args[0].find("a1") > -1
    return True
 $$ LANGUAGE plpythonu;
index e54dca9e2ef00c85b4c08b032fdaff96c71b076c..a09df68c7d13a1216914f426b6eccf9a47f17846 100644 (file)
@@ -26,7 +26,7 @@ CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text
 try:
    rv = plpy.execute(SD["myplan"], [a])
    return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
-except Exception, ex:
+except Exception as ex:
    plpy.error(str(ex))
 return None
 '
@@ -39,7 +39,7 @@ CREATE FUNCTION spi_prepared_plan_test_two(a text) RETURNS text
 try:
    rv = SD["myplan"].execute([a])
    return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
-except Exception, ex:
+except Exception as ex:
    plpy.error(str(ex))
 return None
 '
@@ -53,7 +53,7 @@ try:
    rv = plpy.execute(SD["myplan"])
    if len(rv):
        return rv[0]["count"]
-except Exception, ex:
+except Exception as ex:
    plpy.error(str(ex))
 return None
 '
index 8df64e7619cc39655d04684961f40bc06c43b9a1..0d0ff2e36d9ec34176a8806cc08c5d5787c3f7d8 100644 (file)
@@ -66,7 +66,7 @@ with plpy.subtransaction():
         with plpy.subtransaction():
             plpy.execute("INSERT INTO subtransaction_tbl VALUES (3)")
             plpy.execute("error")
-    except plpy.SPIError, e:
+    except plpy.SPIError as e:
         if not swallow:
             raise
         plpy.notice("Swallowed %s(%r)" % (e.__class__.__name__, e.args[0]))
index 98b89b7d5c11d1045fc21cdb2e04983a8b07c307..0a2659fe29297142ba9a728931fe9d95ed4b9449 100644 (file)
@@ -400,7 +400,7 @@ CREATE FUNCTION test_type_unmarshal(x bytea) RETURNS text AS $$
 import marshal
 try:
     return marshal.loads(x)
-except ValueError, e:
+except ValueError as e:
     return 'FAILED: ' + str(e)
 $$ LANGUAGE plpythonu;
 SELECT test_type_unmarshal(x) FROM test_type_marshal() x;
index 63948159bb4971a182976e7de5ba9e2df17dddb5..e18cb821540d4131d6f662436fc5b6d82db5b0f4 100644 (file)
@@ -14,7 +14,7 @@ REGRESS := $(foreach test,$(REGRESS),$(if $(filter $(test),$(REGRESS_PLPYTHON3_M
 pgregress-python3-mangle:
    $(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 \
-     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 'long'>/<class 'int'>/g" \
          -e "s/\([0-9][0-9]*\)L/\1/g" \
index 889293d33c93d7c0dbb3a492226fb70b6b7b2984..58df2057ef5f6e5b768ac127782e9364ce1c8faa 100644 (file)
@@ -125,7 +125,7 @@ $$;
 DO $$
 try:
     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)
     raise e
 $$ LANGUAGE plpythonu;
@@ -133,7 +133,7 @@ $$ LANGUAGE plpythonu;
 DO $$
 try:
     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))
     raise e
 $$ LANGUAGE plpythonu;
index d712eb1078f849d55fdadcdc1c4a4b76d5fd2d65..88d6936fd0dc8a2ccb755f84274bce15890226eb 100644 (file)
@@ -82,7 +82,7 @@ CREATE FUNCTION invalid_type_caught(a text) RETURNS text
    q = "SELECT fname FROM users WHERE lname = $1"
    try:
        SD["plan"] = plpy.prepare(q, [ "test" ])
-   except plpy.SPIError, ex:
+   except plpy.SPIError as ex:
        plpy.notice(str(ex))
        return None
 rv = plpy.execute(SD["plan"], [ a ])
@@ -104,7 +104,7 @@ CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
    q = "SELECT fname FROM users WHERE lname = $1"
    try:
        SD["plan"] = plpy.prepare(q, [ "test" ])
-   except plpy.SPIError, ex:
+   except plpy.SPIError as ex:
        plpy.error(str(ex))
 rv = plpy.execute(SD["plan"], [ a ])
 if len(rv):
@@ -247,9 +247,9 @@ $$
 from plpy import spiexceptions
 try:
     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)
-except spiexceptions.UniqueViolation, e:
+except spiexceptions.UniqueViolation as e:
     plpy.notice("Violated the UNIQUE constraint, sqlstate %s" % e.sqlstate)
 $$ LANGUAGE plpythonu;
 
index d4a4a24af4f6f6e40d9c4c68aada3388592e5b0b..ec887677e1ea95230bfc7ff823520973f1666a08 100644 (file)
@@ -24,7 +24,7 @@ CREATE FUNCTION import_succeed() returns text
   import re
   import string
   import time
-except Exception, ex:
+except Exception as ex:
    plpy.notice("import failed -- %s" % str(ex))
    return "failed, that wasn''t supposed to happen"
 return "succeeded, as expected"'
index f580abe53d036ddc94a0af8b107a6f4a7b7b7161..ee75c4dc410424abb3d4e74af2a49d94030f95eb 100644 (file)
@@ -29,7 +29,7 @@ CREATE FUNCTION test_param_names3(a0 integer) RETURNS boolean AS $$
 try:
    assert a1 == args[0]
    return False
-except NameError, e:
+except NameError as e:
    assert e.args[0].find("a1") > -1
    return True
 $$ LANGUAGE plpythonu;
index fcf049cb66dc704da90798b4deb91a02aa6e6c9f..dd77833ed56b5fd33cc4a095278ec2a00e578f47 100644 (file)
@@ -31,7 +31,7 @@ CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text
 try:
    rv = plpy.execute(SD["myplan"], [a])
    return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
-except Exception, ex:
+except Exception as ex:
    plpy.error(str(ex))
 return None
 '
@@ -45,7 +45,7 @@ CREATE FUNCTION spi_prepared_plan_test_two(a text) RETURNS text
 try:
    rv = SD["myplan"].execute([a])
    return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
-except Exception, ex:
+except Exception as ex:
    plpy.error(str(ex))
 return None
 '
@@ -60,7 +60,7 @@ try:
    rv = plpy.execute(SD["myplan"])
    if len(rv):
        return rv[0]["count"]
-except Exception, ex:
+except Exception as ex:
    plpy.error(str(ex))
 return None
 '
index 38c861782856921a49c76d2a507855488daa691b..47bb11f157744e44aabc530459988cb16b80890e 100644 (file)
@@ -40,7 +40,7 @@ with plpy.subtransaction():
         with plpy.subtransaction():
             plpy.execute("INSERT INTO subtransaction_tbl VALUES (3)")
             plpy.execute("error")
-    except plpy.SPIError, e:
+    except plpy.SPIError as e:
         if not swallow:
             raise
         plpy.notice("Swallowed %s(%r)" % (e.__class__.__name__, e.args[0]))
index cc0524ee806bddcc94d031d6c39ac7a132728060..0d207d9c0158c265a36de8a63991404940e07e35 100644 (file)
@@ -163,7 +163,7 @@ CREATE FUNCTION test_type_unmarshal(x bytea) RETURNS text AS $$
 import marshal
 try:
     return marshal.loads(x)
-except ValueError, e:
+except ValueError as e:
     return 'FAILED: ' + str(e)
 $$ LANGUAGE plpythonu;
 
index 8a0f132199ff49b24a975e8aa8e2612c1baf0181..7915ee79256d3e8e65f8a3eace4859b053bbd0df 100644 (file)
@@ -290,7 +290,6 @@ sub mangle_plpython3
                close($handle);
                do
                {
-                   s/except ([[:alpha:]][[:alpha:].]*), *([[:alpha:]][[:alpha:]]*):/except $1 as $2:/g;
                    s/<type 'exceptions\.([[:alpha:]]*)'>/<class '$1'>/g;
                    s/<type 'long'>/<class 'int'>/g;
                    s/([0-9][0-9]*)L/$1/g;