From 5cba1aba65f3a74c1e87fe5da56c561f9db6d9d4 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 9 Dec 2019 11:14:02 +0100 Subject: [PATCH 2/2] Modernize Python exception syntax Change the exception syntax used in test and documentation 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. --- doc/src/sgml/plpython.sgml | 8 ++++---- src/pl/plpython/expected/plpython_ereport.out | 4 ++-- src/pl/plpython/expected/plpython_error.out | 8 ++++---- src/pl/plpython/expected/plpython_import.out | 2 +- src/pl/plpython/expected/plpython_params.out | 2 +- src/pl/plpython/expected/plpython_spi.out | 6 +++--- src/pl/plpython/expected/plpython_subtransaction.out | 2 +- src/pl/plpython/expected/plpython_types.out | 2 +- src/pl/plpython/regress-python3-mangle.mk | 2 +- src/pl/plpython/sql/plpython_ereport.sql | 4 ++-- src/pl/plpython/sql/plpython_error.sql | 8 ++++---- src/pl/plpython/sql/plpython_import.sql | 2 +- src/pl/plpython/sql/plpython_params.sql | 2 +- src/pl/plpython/sql/plpython_spi.sql | 6 +++--- src/pl/plpython/sql/plpython_subtransaction.sql | 2 +- src/pl/plpython/sql/plpython_types.sql | 2 +- src/tools/msvc/vcregress.pl | 1 - 17 files changed, 31 insertions(+), 32 deletions(-) diff --git a/doc/src/sgml/plpython.sgml b/doc/src/sgml/plpython.sgml index 31a0dd3e40..7bdaf76bba 100644 --- a/doc/src/sgml/plpython.sgml +++ b/doc/src/sgml/plpython.sgml @@ -1227,7 +1227,7 @@ Trapping Errors return "denominator cannot equal zero" except spiexceptions.UniqueViolation: return "already have that fraction" -except plpy.SPIError, e: +except plpy.SPIError as e: return "other error, SQLSTATE %s" % e.sqlstate else: return "fraction inserted" @@ -1274,7 +1274,7 @@ Subtransaction Context Managers try: plpy.execute("UPDATE accounts SET balance = balance - 100 WHERE account_name = 'joe'") plpy.execute("UPDATE accounts SET balance = balance + 100 WHERE account_name = 'mary'") -except plpy.SPIError, e: +except plpy.SPIError as e: result = "error transferring funds: %s" % e.args else: result = "funds transferred correctly" @@ -1306,7 +1306,7 @@ Subtransaction Context Managers with plpy.subtransaction(): plpy.execute("UPDATE accounts SET balance = balance - 100 WHERE account_name = 'joe'") plpy.execute("UPDATE accounts SET balance = balance + 100 WHERE account_name = 'mary'") -except plpy.SPIError, e: +except plpy.SPIError as e: result = "error transferring funds: %s" % e.args else: result = "funds transferred correctly" @@ -1356,7 +1356,7 @@ Older Python Versions raise else: subxact.exit(None, None, None) -except plpy.SPIError, e: +except plpy.SPIError as e: result = "error transferring funds: %s" % e.args else: result = "funds transferred correctly" diff --git a/src/pl/plpython/expected/plpython_ereport.out b/src/pl/plpython/expected/plpython_ereport.out index e11999ce8c..b73bfff511 100644 --- a/src/pl/plpython/expected/plpython_ereport.out +++ b/src/pl/plpython/expected/plpython_ereport.out @@ -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; diff --git a/src/pl/plpython/expected/plpython_error.out b/src/pl/plpython/expected/plpython_error.out index 4d615b41cc..b2f8fe83eb 100644 --- a/src/pl/plpython/expected/plpython_error.out +++ b/src/pl/plpython/expected/plpython_error.out @@ -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); diff --git a/src/pl/plpython/expected/plpython_import.out b/src/pl/plpython/expected/plpython_import.out index 1d981eacf1..b59e1821a7 100644 --- a/src/pl/plpython/expected/plpython_import.out +++ b/src/pl/plpython/expected/plpython_import.out @@ -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"' diff --git a/src/pl/plpython/expected/plpython_params.out b/src/pl/plpython/expected/plpython_params.out index 8dc3802513..46ea7dfb90 100644 --- a/src/pl/plpython/expected/plpython_params.out +++ b/src/pl/plpython/expected/plpython_params.out @@ -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; diff --git a/src/pl/plpython/expected/plpython_spi.out b/src/pl/plpython/expected/plpython_spi.out index e54dca9e2e..a09df68c7d 100644 --- a/src/pl/plpython/expected/plpython_spi.out +++ b/src/pl/plpython/expected/plpython_spi.out @@ -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 ' diff --git a/src/pl/plpython/expected/plpython_subtransaction.out b/src/pl/plpython/expected/plpython_subtransaction.out index 8df64e7619..0d0ff2e36d 100644 --- a/src/pl/plpython/expected/plpython_subtransaction.out +++ b/src/pl/plpython/expected/plpython_subtransaction.out @@ -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])) diff --git a/src/pl/plpython/expected/plpython_types.out b/src/pl/plpython/expected/plpython_types.out index 98b89b7d5c..0a2659fe29 100644 --- a/src/pl/plpython/expected/plpython_types.out +++ b/src/pl/plpython/expected/plpython_types.out @@ -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; diff --git a/src/pl/plpython/regress-python3-mangle.mk b/src/pl/plpython/regress-python3-mangle.mk index 63948159bb..e18cb82154 100644 --- a/src/pl/plpython/regress-python3-mangle.mk +++ b/src/pl/plpython/regress-python3-mangle.mk @@ -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///g" \ -e "s///g" \ -e "s/\([0-9][0-9]*\)L/\1/g" \ diff --git a/src/pl/plpython/sql/plpython_ereport.sql b/src/pl/plpython/sql/plpython_ereport.sql index 889293d33c..58df2057ef 100644 --- a/src/pl/plpython/sql/plpython_ereport.sql +++ b/src/pl/plpython/sql/plpython_ereport.sql @@ -125,7 +125,7 @@ CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT N 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 @@ CREATE OR REPLACE FUNCTION raise_exception(_message text, _detail text DEFAULT N 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; diff --git a/src/pl/plpython/sql/plpython_error.sql b/src/pl/plpython/sql/plpython_error.sql index d712eb1078..88d6936fd0 100644 --- a/src/pl/plpython/sql/plpython_error.sql +++ b/src/pl/plpython/sql/plpython_error.sql @@ -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 @@ CREATE FUNCTION specific_exception(i integer) RETURNS void AS 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; diff --git a/src/pl/plpython/sql/plpython_import.sql b/src/pl/plpython/sql/plpython_import.sql index d4a4a24af4..ec887677e1 100644 --- a/src/pl/plpython/sql/plpython_import.sql +++ b/src/pl/plpython/sql/plpython_import.sql @@ -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"' diff --git a/src/pl/plpython/sql/plpython_params.sql b/src/pl/plpython/sql/plpython_params.sql index f580abe53d..ee75c4dc41 100644 --- a/src/pl/plpython/sql/plpython_params.sql +++ b/src/pl/plpython/sql/plpython_params.sql @@ -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; diff --git a/src/pl/plpython/sql/plpython_spi.sql b/src/pl/plpython/sql/plpython_spi.sql index fcf049cb66..dd77833ed5 100644 --- a/src/pl/plpython/sql/plpython_spi.sql +++ b/src/pl/plpython/sql/plpython_spi.sql @@ -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 @@ CREATE FUNCTION spi_prepared_plan_test_nested(a text) RETURNS text 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 ' diff --git a/src/pl/plpython/sql/plpython_subtransaction.sql b/src/pl/plpython/sql/plpython_subtransaction.sql index 38c8617828..47bb11f157 100644 --- a/src/pl/plpython/sql/plpython_subtransaction.sql +++ b/src/pl/plpython/sql/plpython_subtransaction.sql @@ -40,7 +40,7 @@ CREATE FUNCTION subtransaction_nested_test(swallow boolean = 'f') RETURNS text 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])) diff --git a/src/pl/plpython/sql/plpython_types.sql b/src/pl/plpython/sql/plpython_types.sql index cc0524ee80..0d207d9c01 100644 --- a/src/pl/plpython/sql/plpython_types.sql +++ b/src/pl/plpython/sql/plpython_types.sql @@ -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; diff --git a/src/tools/msvc/vcregress.pl b/src/tools/msvc/vcregress.pl index 8a0f132199..7915ee7925 100644 --- a/src/tools/msvc/vcregress.pl +++ b/src/tools/msvc/vcregress.pl @@ -290,7 +290,6 @@ sub mangle_plpython3 close($handle); do { - s/except ([[:alpha:]][[:alpha:].]*), *([[:alpha:]][[:alpha:]]*):/except $1 as $2:/g; s///g; s///g; s/([0-9][0-9]*)L/$1/g; -- 2.24.0