From 5dff93638c4443d3afba017f64c9ade69e0fbd3b Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Mon, 24 Aug 2009 20:25:25 +0000
Subject: 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.
---
 src/pl/plpython/sql/plpython_import.sql | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

(limited to 'src/pl/plpython/sql/plpython_import.sql')

diff --git a/src/pl/plpython/sql/plpython_import.sql b/src/pl/plpython/sql/plpython_import.sql
index 7830dd7362..477af328d1 100644
--- a/src/pl/plpython/sql/plpython_import.sql
+++ b/src/pl/plpython/sql/plpython_import.sql
@@ -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;
 
-- 
cgit v1.2.1

