Commit 5dff9363 authored by Peter Eisentraut's avatar Peter Eisentraut

Make PL/Python tests more compatible with Python 3

This changes a bunch of incidentially used constructs in the PL/Python
regression tests to equivalent constructs in cases where Python 3 no longer
supports the old syntax.  Support for older Python versions is unchanged.
parent 8bed238c
...@@ -45,7 +45,7 @@ CONTEXT: PL/Python function "exception_index_invalid_nested" ...@@ -45,7 +45,7 @@ CONTEXT: PL/Python function "exception_index_invalid_nested"
*/ */
CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
q = "SELECT fname FROM users WHERE lname = $1" q = "SELECT fname FROM users WHERE lname = $1"
SD["plan"] = plpy.prepare(q, [ "test" ]) SD["plan"] = plpy.prepare(q, [ "test" ])
rv = plpy.execute(SD["plan"], [ a ]) rv = plpy.execute(SD["plan"], [ a ])
...@@ -64,7 +64,7 @@ CONTEXT: PL/Python function "invalid_type_uncaught" ...@@ -64,7 +64,7 @@ CONTEXT: PL/Python function "invalid_type_uncaught"
*/ */
CREATE FUNCTION invalid_type_caught(a text) RETURNS text CREATE FUNCTION invalid_type_caught(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
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" ])
...@@ -87,7 +87,7 @@ CONTEXT: PL/Python function "invalid_type_caught" ...@@ -87,7 +87,7 @@ CONTEXT: PL/Python function "invalid_type_caught"
*/ */
CREATE FUNCTION invalid_type_reraised(a text) RETURNS text CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
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" ])
...@@ -108,7 +108,7 @@ CONTEXT: PL/Python function "invalid_type_reraised" ...@@ -108,7 +108,7 @@ CONTEXT: PL/Python function "invalid_type_reraised"
*/ */
CREATE FUNCTION valid_type(a text) RETURNS text CREATE FUNCTION valid_type(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
SD["plan"] = plpy.prepare("SELECT fname FROM users WHERE lname = $1", [ "text" ]) SD["plan"] = plpy.prepare("SELECT fname FROM users WHERE lname = $1", [ "text" ])
rv = plpy.execute(SD["plan"], [ a ]) rv = plpy.execute(SD["plan"], [ a ])
if len(rv): if len(rv):
......
...@@ -45,7 +45,7 @@ CONTEXT: PL/Python function "exception_index_invalid_nested" ...@@ -45,7 +45,7 @@ CONTEXT: PL/Python function "exception_index_invalid_nested"
*/ */
CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
q = "SELECT fname FROM users WHERE lname = $1" q = "SELECT fname FROM users WHERE lname = $1"
SD["plan"] = plpy.prepare(q, [ "test" ]) SD["plan"] = plpy.prepare(q, [ "test" ])
rv = plpy.execute(SD["plan"], [ a ]) rv = plpy.execute(SD["plan"], [ a ])
...@@ -64,7 +64,7 @@ CONTEXT: PL/Python function "invalid_type_uncaught" ...@@ -64,7 +64,7 @@ CONTEXT: PL/Python function "invalid_type_uncaught"
*/ */
CREATE FUNCTION invalid_type_caught(a text) RETURNS text CREATE FUNCTION invalid_type_caught(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
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" ])
...@@ -87,7 +87,7 @@ CONTEXT: PL/Python function "invalid_type_caught" ...@@ -87,7 +87,7 @@ CONTEXT: PL/Python function "invalid_type_caught"
*/ */
CREATE FUNCTION invalid_type_reraised(a text) RETURNS text CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
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" ])
...@@ -108,7 +108,7 @@ CONTEXT: PL/Python function "invalid_type_reraised" ...@@ -108,7 +108,7 @@ CONTEXT: PL/Python function "invalid_type_reraised"
*/ */
CREATE FUNCTION valid_type(a text) RETURNS text CREATE FUNCTION valid_type(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
SD["plan"] = plpy.prepare("SELECT fname FROM users WHERE lname = $1", [ "text" ]) SD["plan"] = plpy.prepare("SELECT fname FROM users WHERE lname = $1", [ "text" ])
rv = plpy.execute(SD["plan"], [ a ]) rv = plpy.execute(SD["plan"], [ a ])
if len(rv): if len(rv):
......
...@@ -3,23 +3,23 @@ ...@@ -3,23 +3,23 @@
-- --
CREATE FUNCTION global_test_one() returns text CREATE FUNCTION global_test_one() returns text
AS AS
'if not SD.has_key("global_test"): 'if "global_test" not in SD:
SD["global_test"] = "set by global_test_one" SD["global_test"] = "set by global_test_one"
if not GD.has_key("global_test"): if "global_test" not in GD:
GD["global_test"] = "set by global_test_one" GD["global_test"] = "set by global_test_one"
return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]' return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
LANGUAGE plpythonu; LANGUAGE plpythonu;
CREATE FUNCTION global_test_two() returns text CREATE FUNCTION global_test_two() returns text
AS AS
'if not SD.has_key("global_test"): 'if "global_test" not in SD:
SD["global_test"] = "set by global_test_two" SD["global_test"] = "set by global_test_two"
if not GD.has_key("global_test"): if "global_test" not in GD:
GD["global_test"] = "set by global_test_two" GD["global_test"] = "set by global_test_two"
return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]' return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
LANGUAGE plpythonu; LANGUAGE plpythonu;
CREATE FUNCTION static_test() returns int4 CREATE FUNCTION static_test() returns int4
AS AS
'if SD.has_key("call"): 'if "call" in SD:
SD["call"] = SD["call"] + 1 SD["call"] = SD["call"] + 1
else: else:
SD["call"] = 1 SD["call"] = 1
......
...@@ -17,11 +17,9 @@ CREATE FUNCTION import_succeed() returns text ...@@ -17,11 +17,9 @@ CREATE FUNCTION import_succeed() returns text
import cmath import cmath
import errno import errno
import math import math
import md5
import operator import operator
import random import random
import re import re
import sha
import string import string
import time import time
except Exception, ex: except Exception, ex:
...@@ -31,15 +29,23 @@ return "succeeded, as expected"' ...@@ -31,15 +29,23 @@ return "succeeded, as expected"'
LANGUAGE plpythonu; LANGUAGE plpythonu;
CREATE FUNCTION import_test_one(p text) RETURNS text CREATE FUNCTION import_test_one(p text) RETURNS text
AS AS
'import sha 'try:
digest = sha.new(p) import hashlib
digest = hashlib.sha1(p.encode("ascii"))
except ImportError:
import sha
digest = sha.new(p)
return digest.hexdigest()' return digest.hexdigest()'
LANGUAGE plpythonu; LANGUAGE plpythonu;
CREATE FUNCTION import_test_two(u users) RETURNS text CREATE FUNCTION import_test_two(u users) RETURNS text
AS AS
'import sha 'plain = u["fname"] + u["lname"]
plain = u["fname"] + u["lname"] try:
digest = sha.new(plain); import hashlib
digest = hashlib.sha1(plain.encode("ascii"))
except ImportError:
import sha
digest = sha.new(plain);
return "sha hash of " + plain + " is " + digest.hexdigest()' return "sha hash of " + plain + " is " + digest.hexdigest()'
LANGUAGE plpythonu; LANGUAGE plpythonu;
-- import python modules -- import python modules
......
...@@ -13,7 +13,7 @@ return [ content ]*count ...@@ -13,7 +13,7 @@ return [ content ]*count
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE FUNCTION test_setof_as_tuple(count integer, content text) RETURNS SETOF text AS $$ CREATE FUNCTION test_setof_as_tuple(count integer, content text) RETURNS SETOF text AS $$
t = () t = ()
for i in xrange(count): for i in range(count):
t += ( content, ) t += ( content, )
return t return t
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
......
...@@ -19,7 +19,7 @@ CREATE FUNCTION nested_call_three(a text) RETURNS text ...@@ -19,7 +19,7 @@ CREATE FUNCTION nested_call_three(a text) RETURNS text
-- some spi stuff -- some spi stuff
CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text
AS AS
'if not SD.has_key("myplan"): 'if "myplan" not in SD:
q = "SELECT count(*) FROM users WHERE lname = $1" q = "SELECT count(*) FROM users WHERE lname = $1"
SD["myplan"] = plpy.prepare(q, [ "text" ]) SD["myplan"] = plpy.prepare(q, [ "text" ])
try: try:
...@@ -32,7 +32,7 @@ return None ...@@ -32,7 +32,7 @@ return None
LANGUAGE plpythonu; LANGUAGE plpythonu;
CREATE FUNCTION spi_prepared_plan_test_nested(a text) RETURNS text CREATE FUNCTION spi_prepared_plan_test_nested(a text) RETURNS text
AS AS
'if not SD.has_key("myplan"): 'if "myplan" not in SD:
q = "SELECT spi_prepared_plan_test_one(''%s'') as count" % a q = "SELECT spi_prepared_plan_test_one(''%s'') as count" % a
SD["myplan"] = plpy.prepare(q) SD["myplan"] = plpy.prepare(q)
try: try:
......
...@@ -10,7 +10,7 @@ select stupid(); ...@@ -10,7 +10,7 @@ select stupid();
-- test multiple arguments -- test multiple arguments
CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text
AS AS
'keys = u.keys() 'keys = list(u.keys())
keys.sort() keys.sort()
out = [] out = []
for key in keys: for key in keys:
......
...@@ -69,10 +69,10 @@ CREATE TABLE trigger_test ...@@ -69,10 +69,10 @@ CREATE TABLE trigger_test
(i int, v text ); (i int, v text );
CREATE FUNCTION trigger_data() returns trigger language plpythonu as $$ CREATE FUNCTION trigger_data() returns trigger language plpythonu as $$
if TD.has_key('relid'): if 'relid' in TD:
TD['relid'] = "bogus:12345" TD['relid'] = "bogus:12345"
skeys = TD.keys() skeys = list(TD.keys())
skeys.sort() skeys.sort()
for key in skeys: for key in skeys:
val = TD[key] val = TD[key]
......
...@@ -37,7 +37,7 @@ SELECT exception_index_invalid_nested(); ...@@ -37,7 +37,7 @@ SELECT exception_index_invalid_nested();
*/ */
CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
q = "SELECT fname FROM users WHERE lname = $1" q = "SELECT fname FROM users WHERE lname = $1"
SD["plan"] = plpy.prepare(q, [ "test" ]) SD["plan"] = plpy.prepare(q, [ "test" ])
rv = plpy.execute(SD["plan"], [ a ]) rv = plpy.execute(SD["plan"], [ a ])
...@@ -55,7 +55,7 @@ SELECT invalid_type_uncaught('rick'); ...@@ -55,7 +55,7 @@ SELECT invalid_type_uncaught('rick');
*/ */
CREATE FUNCTION invalid_type_caught(a text) RETURNS text CREATE FUNCTION invalid_type_caught(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
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" ])
...@@ -77,7 +77,7 @@ SELECT invalid_type_caught('rick'); ...@@ -77,7 +77,7 @@ SELECT invalid_type_caught('rick');
*/ */
CREATE FUNCTION invalid_type_reraised(a text) RETURNS text CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
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" ])
...@@ -97,7 +97,7 @@ SELECT invalid_type_reraised('rick'); ...@@ -97,7 +97,7 @@ SELECT invalid_type_reraised('rick');
*/ */
CREATE FUNCTION valid_type(a text) RETURNS text CREATE FUNCTION valid_type(a text) RETURNS text
AS AS
'if not SD.has_key("plan"): 'if "plan" not in SD:
SD["plan"] = plpy.prepare("SELECT fname FROM users WHERE lname = $1", [ "text" ]) SD["plan"] = plpy.prepare("SELECT fname FROM users WHERE lname = $1", [ "text" ])
rv = plpy.execute(SD["plan"], [ a ]) rv = plpy.execute(SD["plan"], [ a ])
if len(rv): if len(rv):
......
...@@ -4,18 +4,18 @@ ...@@ -4,18 +4,18 @@
CREATE FUNCTION global_test_one() returns text CREATE FUNCTION global_test_one() returns text
AS AS
'if not SD.has_key("global_test"): 'if "global_test" not in SD:
SD["global_test"] = "set by global_test_one" SD["global_test"] = "set by global_test_one"
if not GD.has_key("global_test"): if "global_test" not in GD:
GD["global_test"] = "set by global_test_one" GD["global_test"] = "set by global_test_one"
return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]' return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
LANGUAGE plpythonu; LANGUAGE plpythonu;
CREATE FUNCTION global_test_two() returns text CREATE FUNCTION global_test_two() returns text
AS AS
'if not SD.has_key("global_test"): 'if "global_test" not in SD:
SD["global_test"] = "set by global_test_two" SD["global_test"] = "set by global_test_two"
if not GD.has_key("global_test"): if "global_test" not in GD:
GD["global_test"] = "set by global_test_two" GD["global_test"] = "set by global_test_two"
return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]' return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
LANGUAGE plpythonu; LANGUAGE plpythonu;
...@@ -23,7 +23,7 @@ return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]' ...@@ -23,7 +23,7 @@ return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
CREATE FUNCTION static_test() returns int4 CREATE FUNCTION static_test() returns int4
AS AS
'if SD.has_key("call"): 'if "call" in SD:
SD["call"] = SD["call"] + 1 SD["call"] = SD["call"] + 1
else: else:
SD["call"] = 1 SD["call"] = 1
......
...@@ -20,11 +20,9 @@ CREATE FUNCTION import_succeed() returns text ...@@ -20,11 +20,9 @@ CREATE FUNCTION import_succeed() returns text
import cmath import cmath
import errno import errno
import math import math
import md5
import operator import operator
import random import random
import re import re
import sha
import string import string
import time import time
except Exception, ex: except Exception, ex:
...@@ -35,16 +33,24 @@ return "succeeded, as expected"' ...@@ -35,16 +33,24 @@ return "succeeded, as expected"'
CREATE FUNCTION import_test_one(p text) RETURNS text CREATE FUNCTION import_test_one(p text) RETURNS text
AS AS
'import sha 'try:
digest = sha.new(p) import hashlib
digest = hashlib.sha1(p.encode("ascii"))
except ImportError:
import sha
digest = sha.new(p)
return digest.hexdigest()' return digest.hexdigest()'
LANGUAGE plpythonu; LANGUAGE plpythonu;
CREATE FUNCTION import_test_two(u users) RETURNS text CREATE FUNCTION import_test_two(u users) RETURNS text
AS AS
'import sha 'plain = u["fname"] + u["lname"]
plain = u["fname"] + u["lname"] try:
digest = sha.new(plain); import hashlib
digest = hashlib.sha1(plain.encode("ascii"))
except ImportError:
import sha
digest = sha.new(plain);
return "sha hash of " + plain + " is " + digest.hexdigest()' return "sha hash of " + plain + " is " + digest.hexdigest()'
LANGUAGE plpythonu; LANGUAGE plpythonu;
......
...@@ -15,7 +15,7 @@ $$ LANGUAGE plpythonu; ...@@ -15,7 +15,7 @@ $$ LANGUAGE plpythonu;
CREATE FUNCTION test_setof_as_tuple(count integer, content text) RETURNS SETOF text AS $$ CREATE FUNCTION test_setof_as_tuple(count integer, content text) RETURNS SETOF text AS $$
t = () t = ()
for i in xrange(count): for i in range(count):
t += ( content, ) t += ( content, )
return t return t
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
......
...@@ -25,7 +25,7 @@ CREATE FUNCTION nested_call_three(a text) RETURNS text ...@@ -25,7 +25,7 @@ CREATE FUNCTION nested_call_three(a text) RETURNS text
CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text
AS AS
'if not SD.has_key("myplan"): 'if "myplan" not in SD:
q = "SELECT count(*) FROM users WHERE lname = $1" q = "SELECT count(*) FROM users WHERE lname = $1"
SD["myplan"] = plpy.prepare(q, [ "text" ]) SD["myplan"] = plpy.prepare(q, [ "text" ])
try: try:
...@@ -39,7 +39,7 @@ return None ...@@ -39,7 +39,7 @@ return None
CREATE FUNCTION spi_prepared_plan_test_nested(a text) RETURNS text CREATE FUNCTION spi_prepared_plan_test_nested(a text) RETURNS text
AS AS
'if not SD.has_key("myplan"): 'if "myplan" not in SD:
q = "SELECT spi_prepared_plan_test_one(''%s'') as count" % a q = "SELECT spi_prepared_plan_test_one(''%s'') as count" % a
SD["myplan"] = plpy.prepare(q) SD["myplan"] = plpy.prepare(q)
try: try:
......
...@@ -9,7 +9,7 @@ select stupid(); ...@@ -9,7 +9,7 @@ select stupid();
-- test multiple arguments -- test multiple arguments
CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text
AS AS
'keys = u.keys() 'keys = list(u.keys())
keys.sort() keys.sort()
out = [] out = []
for key in keys: for key in keys:
......
...@@ -69,10 +69,10 @@ CREATE TABLE trigger_test ...@@ -69,10 +69,10 @@ CREATE TABLE trigger_test
CREATE FUNCTION trigger_data() returns trigger language plpythonu as $$ CREATE FUNCTION trigger_data() returns trigger language plpythonu as $$
if TD.has_key('relid'): if 'relid' in TD:
TD['relid'] = "bogus:12345" TD['relid'] = "bogus:12345"
skeys = TD.keys() skeys = list(TD.keys())
skeys.sort() skeys.sort()
for key in skeys: for key in skeys:
val = TD[key] val = TD[key]
......
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