Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 45223fd

Browse files
committed
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: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://www.postgresql.org/message-id/flat/98b69261-298c-13d2-f34d-836fd9c29b21%402ndquadrant.com
1 parent 37f21ed commit 45223fd

16 files changed

+27
-28
lines changed

src/pl/plpython/expected/plpython_ereport.out

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ DETAIL: message:(plpy.Error: message text), detail:(detail text), hint: (hint t
186186
DO $$
187187
try:
188188
plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')")
189-
except Exception, e:
189+
except Exception as e:
190190
plpy.info(e.spidata)
191191
raise e
192192
$$ LANGUAGE plpythonu;
@@ -196,7 +196,7 @@ HINT: some hint
196196
DO $$
197197
try:
198198
plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type')
199-
except Exception, e:
199+
except Exception as e:
200200
plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
201201
raise e
202202
$$ LANGUAGE plpythonu;

src/pl/plpython/expected/plpython_error.out

+4-4
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ CREATE FUNCTION invalid_type_caught(a text) RETURNS text
9797
q = "SELECT fname FROM users WHERE lname = $1"
9898
try:
9999
SD["plan"] = plpy.prepare(q, [ "test" ])
100-
except plpy.SPIError, ex:
100+
except plpy.SPIError as ex:
101101
plpy.notice(str(ex))
102102
return None
103103
rv = plpy.execute(SD["plan"], [ a ])
@@ -122,7 +122,7 @@ CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
122122
q = "SELECT fname FROM users WHERE lname = $1"
123123
try:
124124
SD["plan"] = plpy.prepare(q, [ "test" ])
125-
except plpy.SPIError, ex:
125+
except plpy.SPIError as ex:
126126
plpy.error(str(ex))
127127
rv = plpy.execute(SD["plan"], [ a ])
128128
if len(rv):
@@ -321,9 +321,9 @@ $$
321321
from plpy import spiexceptions
322322
try:
323323
plpy.execute("insert into specific values (%s)" % (i or "NULL"));
324-
except spiexceptions.NotNullViolation, e:
324+
except spiexceptions.NotNullViolation as e:
325325
plpy.notice("Violated the NOT NULL constraint, sqlstate %s" % e.sqlstate)
326-
except spiexceptions.UniqueViolation, e:
326+
except spiexceptions.UniqueViolation as e:
327327
plpy.notice("Violated the UNIQUE constraint, sqlstate %s" % e.sqlstate)
328328
$$ LANGUAGE plpythonu;
329329
SELECT specific_exception(2);

src/pl/plpython/expected/plpython_import.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ CREATE FUNCTION import_succeed() returns text
2121
import re
2222
import string
2323
import time
24-
except Exception, ex:
24+
except Exception as ex:
2525
plpy.notice("import failed -- %s" % str(ex))
2626
return "failed, that wasn''t supposed to happen"
2727
return "succeeded, as expected"'

src/pl/plpython/expected/plpython_params.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ CREATE FUNCTION test_param_names3(a0 integer) RETURNS boolean AS $$
2525
try:
2626
assert a1 == args[0]
2727
return False
28-
except NameError, e:
28+
except NameError as e:
2929
assert e.args[0].find("a1") > -1
3030
return True
3131
$$ LANGUAGE plpythonu;

src/pl/plpython/expected/plpython_spi.out

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text
2626
try:
2727
rv = plpy.execute(SD["myplan"], [a])
2828
return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
29-
except Exception, ex:
29+
except Exception as ex:
3030
plpy.error(str(ex))
3131
return None
3232
'
@@ -39,7 +39,7 @@ CREATE FUNCTION spi_prepared_plan_test_two(a text) RETURNS text
3939
try:
4040
rv = SD["myplan"].execute([a])
4141
return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
42-
except Exception, ex:
42+
except Exception as ex:
4343
plpy.error(str(ex))
4444
return None
4545
'
@@ -53,7 +53,7 @@ try:
5353
rv = plpy.execute(SD["myplan"])
5454
if len(rv):
5555
return rv[0]["count"]
56-
except Exception, ex:
56+
except Exception as ex:
5757
plpy.error(str(ex))
5858
return None
5959
'

src/pl/plpython/expected/plpython_subtransaction.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ with plpy.subtransaction():
6666
with plpy.subtransaction():
6767
plpy.execute("INSERT INTO subtransaction_tbl VALUES (3)")
6868
plpy.execute("error")
69-
except plpy.SPIError, e:
69+
except plpy.SPIError as e:
7070
if not swallow:
7171
raise
7272
plpy.notice("Swallowed %s(%r)" % (e.__class__.__name__, e.args[0]))

src/pl/plpython/expected/plpython_types.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ CREATE FUNCTION test_type_unmarshal(x bytea) RETURNS text AS $$
400400
import marshal
401401
try:
402402
return marshal.loads(x)
403-
except ValueError, e:
403+
except ValueError as e:
404404
return 'FAILED: ' + str(e)
405405
$$ LANGUAGE plpythonu;
406406
SELECT test_type_unmarshal(x) FROM test_type_marshal() x;

src/pl/plpython/regress-python3-mangle.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ REGRESS := $(foreach test,$(REGRESS),$(if $(filter $(test),$(REGRESS_PLPYTHON3_M
1414
pgregress-python3-mangle:
1515
$(MKDIR_P) sql/python3 expected/python3 results/python3
1616
for file in $(patsubst %,$(srcdir)/sql/%.sql,$(REGRESS_PLPYTHON3_MANGLE)) $(patsubst %,$(srcdir)/expected/%*.out,$(REGRESS_PLPYTHON3_MANGLE)); do \
17-
sed -e 's/except \([[:alpha:]][[:alpha:].]*\), *\([[:alpha:]][[:alpha:]]*\):/except \1 as \2:/g' \
17+
sed \
1818
-e "s/<type 'exceptions\.\([[:alpha:]]*\)'>/<class '\1'>/g" \
1919
-e "s/<type 'long'>/<class 'int'>/g" \
2020
-e "s/\([0-9][0-9]*\)L/\1/g" \

src/pl/plpython/sql/plpython_ereport.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,15 @@ $$;
125125
DO $$
126126
try:
127127
plpy.execute("select raise_exception(_message => 'my message', _sqlstate => 'XX987', _hint => 'some hint', _table_name => 'users_tab', _datatype_name => 'user_type')")
128-
except Exception, e:
128+
except Exception as e:
129129
plpy.info(e.spidata)
130130
raise e
131131
$$ LANGUAGE plpythonu;
132132

133133
DO $$
134134
try:
135135
plpy.error(message = 'my message', sqlstate = 'XX987', hint = 'some hint', table_name = 'users_tab', datatype_name = 'user_type')
136-
except Exception, e:
136+
except Exception as e:
137137
plpy.info('sqlstate: %s, hint: %s, table_name: %s, datatype_name: %s' % (e.sqlstate, e.hint, e.table_name, e.datatype_name))
138138
raise e
139139
$$ LANGUAGE plpythonu;

src/pl/plpython/sql/plpython_error.sql

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ CREATE FUNCTION invalid_type_caught(a text) RETURNS text
8282
q = "SELECT fname FROM users WHERE lname = $1"
8383
try:
8484
SD["plan"] = plpy.prepare(q, [ "test" ])
85-
except plpy.SPIError, ex:
85+
except plpy.SPIError as ex:
8686
plpy.notice(str(ex))
8787
return None
8888
rv = plpy.execute(SD["plan"], [ a ])
@@ -104,7 +104,7 @@ CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
104104
q = "SELECT fname FROM users WHERE lname = $1"
105105
try:
106106
SD["plan"] = plpy.prepare(q, [ "test" ])
107-
except plpy.SPIError, ex:
107+
except plpy.SPIError as ex:
108108
plpy.error(str(ex))
109109
rv = plpy.execute(SD["plan"], [ a ])
110110
if len(rv):
@@ -247,9 +247,9 @@ $$
247247
from plpy import spiexceptions
248248
try:
249249
plpy.execute("insert into specific values (%s)" % (i or "NULL"));
250-
except spiexceptions.NotNullViolation, e:
250+
except spiexceptions.NotNullViolation as e:
251251
plpy.notice("Violated the NOT NULL constraint, sqlstate %s" % e.sqlstate)
252-
except spiexceptions.UniqueViolation, e:
252+
except spiexceptions.UniqueViolation as e:
253253
plpy.notice("Violated the UNIQUE constraint, sqlstate %s" % e.sqlstate)
254254
$$ LANGUAGE plpythonu;
255255

src/pl/plpython/sql/plpython_import.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ CREATE FUNCTION import_succeed() returns text
2424
import re
2525
import string
2626
import time
27-
except Exception, ex:
27+
except Exception as ex:
2828
plpy.notice("import failed -- %s" % str(ex))
2929
return "failed, that wasn''t supposed to happen"
3030
return "succeeded, as expected"'

src/pl/plpython/sql/plpython_params.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ CREATE FUNCTION test_param_names3(a0 integer) RETURNS boolean AS $$
2929
try:
3030
assert a1 == args[0]
3131
return False
32-
except NameError, e:
32+
except NameError as e:
3333
assert e.args[0].find("a1") > -1
3434
return True
3535
$$ LANGUAGE plpythonu;

src/pl/plpython/sql/plpython_spi.sql

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text
3131
try:
3232
rv = plpy.execute(SD["myplan"], [a])
3333
return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
34-
except Exception, ex:
34+
except Exception as ex:
3535
plpy.error(str(ex))
3636
return None
3737
'
@@ -45,7 +45,7 @@ CREATE FUNCTION spi_prepared_plan_test_two(a text) RETURNS text
4545
try:
4646
rv = SD["myplan"].execute([a])
4747
return "there are " + str(rv[0]["count"]) + " " + str(a) + "s"
48-
except Exception, ex:
48+
except Exception as ex:
4949
plpy.error(str(ex))
5050
return None
5151
'
@@ -60,7 +60,7 @@ try:
6060
rv = plpy.execute(SD["myplan"])
6161
if len(rv):
6262
return rv[0]["count"]
63-
except Exception, ex:
63+
except Exception as ex:
6464
plpy.error(str(ex))
6565
return None
6666
'

src/pl/plpython/sql/plpython_subtransaction.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ with plpy.subtransaction():
4040
with plpy.subtransaction():
4141
plpy.execute("INSERT INTO subtransaction_tbl VALUES (3)")
4242
plpy.execute("error")
43-
except plpy.SPIError, e:
43+
except plpy.SPIError as e:
4444
if not swallow:
4545
raise
4646
plpy.notice("Swallowed %s(%r)" % (e.__class__.__name__, e.args[0]))

src/pl/plpython/sql/plpython_types.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ CREATE FUNCTION test_type_unmarshal(x bytea) RETURNS text AS $$
163163
import marshal
164164
try:
165165
return marshal.loads(x)
166-
except ValueError, e:
166+
except ValueError as e:
167167
return 'FAILED: ' + str(e)
168168
$$ LANGUAGE plpythonu;
169169

src/tools/msvc/vcregress.pl

-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ sub mangle_plpython3
290290
close($handle);
291291
do
292292
{
293-
s/except ([[:alpha:]][[:alpha:].]*), *([[:alpha:]][[:alpha:]]*):/except $1 as $2:/g;
294293
s/<type 'exceptions\.([[:alpha:]]*)'>/<class '$1'>/g;
295294
s/<type 'long'>/<class 'int'>/g;
296295
s/([0-9][0-9]*)L/$1/g;

0 commit comments

Comments
 (0)