]> granicus.if.org Git - postgresql/commitdiff
Make PL/Python tests more compatible with Python 3
authorPeter Eisentraut <peter_e@gmx.net>
Mon, 24 Aug 2009 20:25:25 +0000 (20:25 +0000)
committerPeter Eisentraut <peter_e@gmx.net>
Mon, 24 Aug 2009 20:25:25 +0000 (20:25 +0000)
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.

15 files changed:
src/pl/plpython/expected/plpython_error.out
src/pl/plpython/expected/plpython_error_2.out
src/pl/plpython/expected/plpython_global.out
src/pl/plpython/expected/plpython_import.out
src/pl/plpython/expected/plpython_setof.out
src/pl/plpython/expected/plpython_spi.out
src/pl/plpython/expected/plpython_test.out
src/pl/plpython/expected/plpython_trigger.out
src/pl/plpython/sql/plpython_error.sql
src/pl/plpython/sql/plpython_global.sql
src/pl/plpython/sql/plpython_import.sql
src/pl/plpython/sql/plpython_setof.sql
src/pl/plpython/sql/plpython_spi.sql
src/pl/plpython/sql/plpython_test.sql
src/pl/plpython/sql/plpython_trigger.sql

index 8454b05e5ea9c900ad17867682e12201bee258f5..58f7b3a766e1310f049aa8d77890fecf3c9fc60f 100644 (file)
@@ -45,7 +45,7 @@ CONTEXT:  PL/Python function "exception_index_invalid_nested"
  */
 CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        q = "SELECT fname FROM users WHERE lname = $1"
        SD["plan"] = plpy.prepare(q, [ "test" ])
 rv = plpy.execute(SD["plan"], [ a ])
@@ -64,7 +64,7 @@ CONTEXT:  PL/Python function "invalid_type_uncaught"
  */
 CREATE FUNCTION invalid_type_caught(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        q = "SELECT fname FROM users WHERE lname = $1"
        try:
                SD["plan"] = plpy.prepare(q, [ "test" ])
@@ -87,7 +87,7 @@ CONTEXT:  PL/Python function "invalid_type_caught"
  */
 CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        q = "SELECT fname FROM users WHERE lname = $1"
        try:
                SD["plan"] = plpy.prepare(q, [ "test" ])
@@ -108,7 +108,7 @@ CONTEXT:  PL/Python function "invalid_type_reraised"
  */
 CREATE FUNCTION valid_type(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        SD["plan"] = plpy.prepare("SELECT fname FROM users WHERE lname = $1", [ "text" ])
 rv = plpy.execute(SD["plan"], [ a ])
 if len(rv):
index 7d41c8a8695e1026658a7f6dce37a66bf11efdbf..bcbf2e655c2f51ba14b9bee192659d68f340c080 100644 (file)
@@ -45,7 +45,7 @@ CONTEXT:  PL/Python function "exception_index_invalid_nested"
  */
 CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        q = "SELECT fname FROM users WHERE lname = $1"
        SD["plan"] = plpy.prepare(q, [ "test" ])
 rv = plpy.execute(SD["plan"], [ a ])
@@ -64,7 +64,7 @@ CONTEXT:  PL/Python function "invalid_type_uncaught"
  */
 CREATE FUNCTION invalid_type_caught(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        q = "SELECT fname FROM users WHERE lname = $1"
        try:
                SD["plan"] = plpy.prepare(q, [ "test" ])
@@ -87,7 +87,7 @@ CONTEXT:  PL/Python function "invalid_type_caught"
  */
 CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        q = "SELECT fname FROM users WHERE lname = $1"
        try:
                SD["plan"] = plpy.prepare(q, [ "test" ])
@@ -108,7 +108,7 @@ CONTEXT:  PL/Python function "invalid_type_reraised"
  */
 CREATE FUNCTION valid_type(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        SD["plan"] = plpy.prepare("SELECT fname FROM users WHERE lname = $1", [ "text" ])
 rv = plpy.execute(SD["plan"], [ a ])
 if len(rv):
index f20014b926edbcc166a1ebdb7cf7b54e02ac3696..192e3e48a72f82f205c527627db909cb60f09094 100644 (file)
@@ -3,23 +3,23 @@
 --
 CREATE FUNCTION global_test_one() returns text
     AS
-'if not SD.has_key("global_test"):
+'if "global_test" not in SD:
        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"
 return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
     LANGUAGE plpythonu;
 CREATE FUNCTION global_test_two() returns text
     AS
-'if not SD.has_key("global_test"):
+'if "global_test" not in SD:
        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"
 return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
     LANGUAGE plpythonu;
 CREATE FUNCTION static_test() returns int4
     AS
-'if SD.has_key("call"):
+'if "call" in SD:
        SD["call"] = SD["call"] + 1
 else:
        SD["call"] = 1
index f1f665e052344e20e1842d9d168fc633e5bcc30b..7fcd9267371b78f3353881945230cb3cc71e94ec 100644 (file)
@@ -17,11 +17,9 @@ CREATE FUNCTION import_succeed() returns text
   import cmath
   import errno
   import math
-  import md5
   import operator
   import random
   import re
-  import sha
   import string
   import time
 except Exception, ex:
@@ -31,15 +29,23 @@ return "succeeded, as expected"'
     LANGUAGE plpythonu;
 CREATE FUNCTION import_test_one(p text) RETURNS text
        AS
-'import sha
-digest = sha.new(p)
+'try:
+    import hashlib
+    digest = hashlib.sha1(p.encode("ascii"))
+except ImportError:
+    import sha
+    digest = sha.new(p)
 return digest.hexdigest()'
        LANGUAGE plpythonu;
 CREATE FUNCTION import_test_two(u users) RETURNS text
        AS
-'import sha
-plain = u["fname"] + u["lname"]
-digest = sha.new(plain);
+'plain = u["fname"] + u["lname"]
+try:
+    import hashlib
+    digest = hashlib.sha1(plain.encode("ascii"))
+except ImportError:
+    import sha
+    digest = sha.new(plain);
 return "sha hash of " + plain + " is " + digest.hexdigest()'
        LANGUAGE plpythonu;
 -- import python modules
index 03a97194c827e49614cf6ad3b13e20e22d73cb4f..ebf896df01f1db4aa538bda08127942b1c03610d 100644 (file)
@@ -13,7 +13,7 @@ return [ content ]*count
 $$ LANGUAGE plpythonu;
 CREATE FUNCTION test_setof_as_tuple(count integer, content text) RETURNS SETOF text AS $$
 t = ()
-for i in xrange(count):
+for i in range(count):
        t += ( content, )
 return t
 $$ LANGUAGE plpythonu;
index f4910d9d52bcd1410420aa21e736039741249e6a..9cef3f2bb0b43a33c3d34412b9d939c449fbc51f 100644 (file)
@@ -19,7 +19,7 @@ CREATE FUNCTION nested_call_three(a text) RETURNS text
 -- some spi stuff
 CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text
        AS
-'if not SD.has_key("myplan"):
+'if "myplan" not in SD:
        q = "SELECT count(*) FROM users WHERE lname = $1"
        SD["myplan"] = plpy.prepare(q, [ "text" ])
 try:
@@ -32,7 +32,7 @@ return None
        LANGUAGE plpythonu;
 CREATE FUNCTION spi_prepared_plan_test_nested(a text) RETURNS text
        AS
-'if not SD.has_key("myplan"):
+'if "myplan" not in SD:
        q = "SELECT spi_prepared_plan_test_one(''%s'') as count" % a
        SD["myplan"] = plpy.prepare(q)
 try:
index 5cce4e290f036131ba04af8995e6724adae5be17..8bef675009b9374ceaf945e005a9000f36bcda32 100644 (file)
@@ -10,7 +10,7 @@ select stupid();
 -- test multiple arguments
 CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text
        AS
-'keys = u.keys()
+'keys = list(u.keys())
 keys.sort()
 out = []
 for key in keys:
index 75914047cea17ff0d44ef4a80da48a656cb9aee0..b60796dab5b07df6c8405bdfa2bb4e9cd92f9a1a 100644 (file)
@@ -69,10 +69,10 @@ CREATE TABLE trigger_test
        (i int, v text );
 CREATE FUNCTION trigger_data() returns trigger language plpythonu as $$
 
-if TD.has_key('relid'):
+if 'relid' in TD:
        TD['relid'] = "bogus:12345"
 
-skeys = TD.keys()
+skeys = list(TD.keys())
 skeys.sort()
 for key in skeys:
        val = TD[key]
index 04161bc25ecf8a855bd620205a8713d39bcb1971..5ca68495be036f86df939d02c45115d312ce99e0 100644 (file)
@@ -37,7 +37,7 @@ SELECT exception_index_invalid_nested();
  */
 CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        q = "SELECT fname FROM users WHERE lname = $1"
        SD["plan"] = plpy.prepare(q, [ "test" ])
 rv = plpy.execute(SD["plan"], [ a ])
@@ -55,7 +55,7 @@ SELECT invalid_type_uncaught('rick');
  */
 CREATE FUNCTION invalid_type_caught(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        q = "SELECT fname FROM users WHERE lname = $1"
        try:
                SD["plan"] = plpy.prepare(q, [ "test" ])
@@ -77,7 +77,7 @@ SELECT invalid_type_caught('rick');
  */
 CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        q = "SELECT fname FROM users WHERE lname = $1"
        try:
                SD["plan"] = plpy.prepare(q, [ "test" ])
@@ -97,7 +97,7 @@ SELECT invalid_type_reraised('rick');
  */
 CREATE FUNCTION valid_type(a text) RETURNS text
        AS
-'if not SD.has_key("plan"):
+'if "plan" not in SD:
        SD["plan"] = plpy.prepare("SELECT fname FROM users WHERE lname = $1", [ "text" ])
 rv = plpy.execute(SD["plan"], [ a ])
 if len(rv):
index e676ad6f438f62e90ac5ba52ddb0687215511875..32502b41eeef9ff1f643589382d422846faa34db 100644 (file)
@@ -4,18 +4,18 @@
 
 CREATE FUNCTION global_test_one() returns text
     AS
-'if not SD.has_key("global_test"):
+'if "global_test" not in SD:
        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"
 return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
     LANGUAGE plpythonu;
 
 CREATE FUNCTION global_test_two() returns text
     AS
-'if not SD.has_key("global_test"):
+'if "global_test" not in SD:
        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"
 return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
     LANGUAGE plpythonu;
@@ -23,7 +23,7 @@ return "SD: " + SD["global_test"] + ", GD: " + GD["global_test"]'
 
 CREATE FUNCTION static_test() returns int4
     AS
-'if SD.has_key("call"):
+'if "call" in SD:
        SD["call"] = SD["call"] + 1
 else:
        SD["call"] = 1
index 7830dd7362dd12ae189b5ec31e4198bb0c1210ed..477af328d18e5d1059945a5aa23c4faa2bde8ea8 100644 (file)
@@ -20,11 +20,9 @@ CREATE FUNCTION import_succeed() returns text
   import cmath
   import errno
   import math
-  import md5
   import operator
   import random
   import re
-  import sha
   import string
   import time
 except Exception, ex:
@@ -35,16 +33,24 @@ return "succeeded, as expected"'
 
 CREATE FUNCTION import_test_one(p text) RETURNS text
        AS
-'import sha
-digest = sha.new(p)
+'try:
+    import hashlib
+    digest = hashlib.sha1(p.encode("ascii"))
+except ImportError:
+    import sha
+    digest = sha.new(p)
 return digest.hexdigest()'
        LANGUAGE plpythonu;
 
 CREATE FUNCTION import_test_two(u users) RETURNS text
        AS
-'import sha
-plain = u["fname"] + u["lname"]
-digest = sha.new(plain);
+'plain = u["fname"] + u["lname"]
+try:
+    import hashlib
+    digest = hashlib.sha1(plain.encode("ascii"))
+except ImportError:
+    import sha
+    digest = sha.new(plain);
 return "sha hash of " + plain + " is " + digest.hexdigest()'
        LANGUAGE plpythonu;
 
index e036d569f2050d8b04eb425bb2082d8d2627cb9e..53d91a9e7d77eb20cb69d8bfdfc1669d9d15507a 100644 (file)
@@ -15,7 +15,7 @@ $$ LANGUAGE plpythonu;
 
 CREATE FUNCTION test_setof_as_tuple(count integer, content text) RETURNS SETOF text AS $$
 t = ()
-for i in xrange(count):
+for i in range(count):
        t += ( content, )
 return t
 $$ LANGUAGE plpythonu;
index c663298525747fc7a08830427aa0633271232c09..2157569f4c78854aec3c7a0fce47d6ab75de30d8 100644 (file)
@@ -25,7 +25,7 @@ CREATE FUNCTION nested_call_three(a text) RETURNS text
 
 CREATE FUNCTION spi_prepared_plan_test_one(a text) RETURNS text
        AS
-'if not SD.has_key("myplan"):
+'if "myplan" not in SD:
        q = "SELECT count(*) FROM users WHERE lname = $1"
        SD["myplan"] = plpy.prepare(q, [ "text" ])
 try:
@@ -39,7 +39,7 @@ return None
 
 CREATE FUNCTION spi_prepared_plan_test_nested(a text) RETURNS text
        AS
-'if not SD.has_key("myplan"):
+'if "myplan" not in SD:
        q = "SELECT spi_prepared_plan_test_one(''%s'') as count" % a
        SD["myplan"] = plpy.prepare(q)
 try:
index d45299420f7047c94b91cb61b4746759a278220e..4a7a687e66681a6ce6c7b47a03d9643c89be2206 100644 (file)
@@ -9,7 +9,7 @@ select stupid();
 -- test multiple arguments
 CREATE FUNCTION argument_test_one(u users, a1 text, a2 text) RETURNS text
        AS
-'keys = u.keys()
+'keys = list(u.keys())
 keys.sort()
 out = []
 for key in keys:
index ce1a737a844f6b635616cd76963b0ecb5726d167..385fa93bda47f01a0aac08eff0aba767bb18206f 100644 (file)
@@ -69,10 +69,10 @@ CREATE TABLE trigger_test
 
 CREATE FUNCTION trigger_data() returns trigger language plpythonu as $$
 
-if TD.has_key('relid'):
+if 'relid' in TD:
        TD['relid'] = "bogus:12345"
 
-skeys = TD.keys()
+skeys = list(TD.keys())
 skeys.sort()
 for key in skeys:
        val = TD[key]