Blame SOURCES/Convert-Python-tests-to-Python-3.patch

c41359
From 2bc365f12282cdd83a191478b97f4ea0d9aa60dd Mon Sep 17 00:00:00 2001
c41359
From: Greg Hudson <ghudson@mit.edu>
c41359
Date: Mon, 19 Feb 2018 21:10:09 -0500
c41359
Subject: [PATCH] Convert Python tests to Python 3
c41359
c41359
Look for python3 in configure.in and verify that we got it.  Convert
c41359
test code to conform to Python 3.
c41359
c41359
ticket: 8710 (new)
c41359
(cherry picked from commit e23d24beacb73581bbf4351250f3955e6fd44361)
c41359
[rharwood@redhat.com: Context skew due to not having LMDB in tests]
c41359
---
c41359
 src/Makefile.in                  |  1 +
c41359
 src/configure.in                 |  6 ++--
c41359
 src/kadmin/dbutil/t_tdumputil.py |  4 +--
c41359
 src/tests/jsonwalker.py          | 16 +++++------
c41359
 src/tests/t_cve-2012-1014.py     |  2 +-
c41359
 src/tests/t_cve-2012-1015.py     |  2 +-
c41359
 src/tests/t_hostrealm.py         |  4 ++-
c41359
 src/tests/t_kdb.py               | 11 ++++---
c41359
 src/tests/t_keytab.py            | 34 +++++++++++-----------
c41359
 src/tests/t_mkey.py              |  6 ++--
c41359
 src/tests/t_otp.py               |  7 +++--
c41359
 src/tests/t_tabdump.py           |  4 +--
c41359
 src/util/Makefile.in             |  1 +
c41359
 src/util/k5test.py               | 49 +++++++++++++++++---------------
c41359
 src/util/princflags.py           | 25 ++++++++--------
c41359
 15 files changed, 88 insertions(+), 84 deletions(-)
c41359
c41359
diff --git a/src/Makefile.in b/src/Makefile.in
c41359
index 77beff8bc..79b8d5f98 100644
c41359
--- a/src/Makefile.in
c41359
+++ b/src/Makefile.in
c41359
@@ -533,6 +533,7 @@ runenv.py: pyrunenv.vals
c41359
 
c41359
 clean-unix::
c41359
 	$(RM) runenv.py runenv.pyc pyrunenv.vals
c41359
+	$(RM) -r __pycache__
c41359
 
c41359
 COV_BUILD=	cov-build
c41359
 COV_ANALYZE=	cov-analyze
c41359
diff --git a/src/configure.in b/src/configure.in
c41359
index 3f45784b5..00cb297b8 100644
c41359
--- a/src/configure.in
c41359
+++ b/src/configure.in
c41359
@@ -1098,15 +1098,13 @@ fi
c41359
 AC_SUBST(HAVE_RUNTEST)
c41359
 
c41359
 # For Python tests.
c41359
-AC_CHECK_PROG(PYTHON,python2,python2)
c41359
+AC_CHECK_PROG(PYTHON,python3,python3)
c41359
 if text x"$PYTHON" = x; then
c41359
 	AC_CHECK_PROG(PYTHON,python,python)
c41359
 fi
c41359
 HAVE_PYTHON=no
c41359
 if test x"$PYTHON" != x; then
c41359
-	# k5test.py requires python 2.4 (for the subprocess module).
c41359
-	# Some code needs python 2.5 (for syntax like conditional expressions).
c41359
-	wantver="(sys.hexversion >= 0x2050000 and sys.hexversion < 0x3000000)"
c41359
+	wantver="(sys.hexversion >= 0x3000000)"
c41359
 	if "$PYTHON" -c "import sys; sys.exit(not $wantver and 1 or 0)"; then
c41359
 		HAVE_PYTHON=yes
c41359
 	fi
c41359
diff --git a/src/kadmin/dbutil/t_tdumputil.py b/src/kadmin/dbutil/t_tdumputil.py
c41359
index 52e356533..47b2aa7a3 100755
c41359
--- a/src/kadmin/dbutil/t_tdumputil.py
c41359
+++ b/src/kadmin/dbutil/t_tdumputil.py
c41359
@@ -6,8 +6,8 @@ realm = K5Realm(create_kdb=False)
c41359
 def compare(s, expected, msg):
c41359
     if s == expected:
c41359
         return
c41359
-    print 'expected:', repr(expected)
c41359
-    print 'got:', repr(s)
c41359
+    print('expected:', repr(expected))
c41359
+    print('got:', repr(s))
c41359
     fail(msg)
c41359
 
c41359
 out = realm.run(['./t_tdumputil', '2', 'field1', 'field2',
c41359
diff --git a/src/tests/jsonwalker.py b/src/tests/jsonwalker.py
c41359
index 942ca2db7..7a0675e08 100644
c41359
--- a/src/tests/jsonwalker.py
c41359
+++ b/src/tests/jsonwalker.py
c41359
@@ -2,8 +2,8 @@ import sys
c41359
 try:
c41359
     import cjson
c41359
 except ImportError:
c41359
-    print "Warning: skipping audit log verification because the cjson module" \
c41359
-          " is unavailable"
c41359
+    print("Warning: skipping audit log verification because the cjson module" \
c41359
+          " is unavailable")
c41359
     sys.exit(0)
c41359
 from collections import defaultdict
c41359
 from optparse import OptionParser
c41359
@@ -22,10 +22,10 @@ class Parser(object):
c41359
         result = self.parse(logs)
c41359
         if len(result) != len(self.defaults):
c41359
             diff = set(self.defaults.keys()).difference(result.keys())
c41359
-            print 'Test failed.'
c41359
-            print 'The following attributes were not set:'
c41359
+            print('Test failed.')
c41359
+            print('The following attributes were not set:')
c41359
             for it in diff:
c41359
-                print it
c41359
+                print(it)
c41359
             sys.exit(1)
c41359
 
c41359
     def flatten(self, defaults):
c41359
@@ -42,7 +42,7 @@ class Parser(object):
c41359
         result = dict()
c41359
         for path,value in self._walk(defaults):
c41359
             if path in result:
c41359
-                print 'Warning: attribute path %s already exists' % path
c41359
+                print('Warning: attribute path %s already exists' % path)
c41359
             result[path] = value
c41359
 
c41359
         return result
c41359
@@ -60,7 +60,7 @@ class Parser(object):
c41359
                         if v is not None:
c41359
                             dv = self.DEFAULTS[type(v)]
c41359
                         else:
c41359
-                            print 'Warning: attribute %s is set to None' % a
c41359
+                            print('Warning: attribute %s is set to None' % a)
c41359
                             continue
c41359
                     # by now we have default value
c41359
                     if v != dv:
c41359
@@ -96,7 +96,7 @@ if __name__ == '__main__':
c41359
                 content.append(cjson.decode(l.rstrip()))
c41359
         f.close()
c41359
     else:
c41359
-        print 'Input file in jason format is required'
c41359
+        print('Input file in jason format is required')
c41359
         exit()
c41359
 
c41359
     defaults = None
c41359
diff --git a/src/tests/t_cve-2012-1014.py b/src/tests/t_cve-2012-1014.py
c41359
index dcff95f6e..8447e0ee7 100755
c41359
--- a/src/tests/t_cve-2012-1014.py
c41359
+++ b/src/tests/t_cve-2012-1014.py
c41359
@@ -20,7 +20,7 @@ x2 = base64.b16decode('A44F304DA007030500FEDCBA90A10E30' +
c41359
                       '01')
c41359
 
c41359
 for x in range(11, 128):
c41359
-    s.sendto(''.join([x1, chr(x), x2]), a)
c41359
+    s.sendto(x1 + bytes([x]) + x2, a)
c41359
 
c41359
 # Make sure kinit still works.
c41359
 
c41359
diff --git a/src/tests/t_cve-2012-1015.py b/src/tests/t_cve-2012-1015.py
c41359
index 28b1e619b..ae5678cac 100755
c41359
--- a/src/tests/t_cve-2012-1015.py
c41359
+++ b/src/tests/t_cve-2012-1015.py
c41359
@@ -27,7 +27,7 @@ x1 = base64.b16decode('6A81A030819DA103020105A20302010A' +
c41359
 x2 = base64.b16decode('A8083006020106020112')
c41359
 
c41359
 for x in range(0, 128):
c41359
-    s.sendto(''.join([x1, chr(x), x2]), a)
c41359
+    s.sendto(x1 + bytes([x]) + x2, a)
c41359
 
c41359
 # Make sure kinit still works.
c41359
 
c41359
diff --git a/src/tests/t_hostrealm.py b/src/tests/t_hostrealm.py
c41359
index 256ba2a38..beea6f3bc 100755
c41359
--- a/src/tests/t_hostrealm.py
c41359
+++ b/src/tests/t_hostrealm.py
c41359
@@ -119,7 +119,9 @@ testd(realm, 'KRBTEST.COM', 'default_realm profile', env=notest2)
c41359
 # see the first.  Remove the profile default_realm setting to expose
c41359
 # this behavior.
c41359
 remove_default = {'libdefaults': {'default_realm': None}}
c41359
-nodefault_conf = dict(disable_conf.items() + remove_default.items())
c41359
+# Python 3.5+: nodefault_conf = {**disable_conf, **remove_default}
c41359
+nodefault_conf = dict(list(disable_conf.items()) +
c41359
+                      list(remove_default.items()))
c41359
 nodefault = realm.special_env('nodefault', False, krb5_conf=nodefault_conf)
c41359
 testd(realm, 'one', 'default_realm test1', env=nodefault)
c41359
 
c41359
diff --git a/src/tests/t_kdb.py b/src/tests/t_kdb.py
c41359
index 983cd93c8..42237f7a1 100755
c41359
--- a/src/tests/t_kdb.py
c41359
+++ b/src/tests/t_kdb.py
c41359
@@ -1,6 +1,5 @@
c41359
 from k5test import *
c41359
 import time
c41359
-from itertools import imap
c41359
 
c41359
 # Run kdbtest against the BDB module.
c41359
 realm = K5Realm(create_kdb=False)
c41359
@@ -51,7 +50,7 @@ else:
c41359
 def slap_add(ldif):
c41359
     proc = subprocess.Popen([slapadd, '-b', 'cn=config', '-F', slapd_conf],
c41359
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
c41359
-                            stderr=subprocess.STDOUT)
c41359
+                            stderr=subprocess.STDOUT, universal_newlines=True)
c41359
     (out, dummy) = proc.communicate(ldif)
c41359
     output(out)
c41359
     return proc.wait()
c41359
@@ -98,7 +97,7 @@ if slap_add('include: file://%s\n' % schema) != 0:
c41359
 ldap_homes = ['/etc/ldap', '/etc/openldap', '/usr/local/etc/openldap',
c41359
               '/usr/local/etc/ldap']
c41359
 local_schema_path = '/schema/core.ldif'
c41359
-core_schema = next((i for i in imap(lambda x:x+local_schema_path, ldap_homes)
c41359
+core_schema = next((i for i in map(lambda x:x+local_schema_path, ldap_homes)
c41359
                     if os.path.isfile(i)), None)
c41359
 if core_schema:
c41359
     if slap_add('include: file://%s\n' % core_schema) != 0:
c41359
@@ -114,7 +113,7 @@ atexit.register(kill_slapd)
c41359
 
c41359
 out = open(slapd_out, 'w')
c41359
 subprocess.call([slapd, '-h', ldap_uri, '-F', slapd_conf], stdout=out,
c41359
-                stderr=out)
c41359
+                stderr=out, universal_newlines=True)
c41359
 out.close()
c41359
 pidf = open(slapd_pidfile, 'r')
c41359
 slapd_pid = int(pidf.read())
c41359
@@ -158,7 +157,7 @@ def ldap_search(args):
c41359
     proc = subprocess.Popen([ldapsearch, '-H', ldap_uri, '-b', top_dn,
c41359
                              '-D', admin_dn, '-w', admin_pw, args],
c41359
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
c41359
-                            stderr=subprocess.STDOUT)
c41359
+                            stderr=subprocess.STDOUT, universal_newlines=True)
c41359
     (out, dummy) = proc.communicate()
c41359
     return out
c41359
 
c41359
@@ -166,7 +165,7 @@ def ldap_modify(ldif, args=[]):
c41359
     proc = subprocess.Popen([ldapmodify, '-H', ldap_uri, '-D', admin_dn,
c41359
                              '-x', '-w', admin_pw] + args,
c41359
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
c41359
-                            stderr=subprocess.STDOUT)
c41359
+                            stderr=subprocess.STDOUT, universal_newlines=True)
c41359
     (out, dummy) = proc.communicate(ldif)
c41359
     output(out)
c41359
 
c41359
diff --git a/src/tests/t_keytab.py b/src/tests/t_keytab.py
c41359
index 228c36334..8a17ae2eb 100755
c41359
--- a/src/tests/t_keytab.py
c41359
+++ b/src/tests/t_keytab.py
c41359
@@ -90,36 +90,36 @@ test_key_rotate(realm, princ, 2)
c41359
 
c41359
 # Test that klist -k can read a keytab entry without a 32-bit kvno and
c41359
 # reports the 8-bit key version.
c41359
-record = '\x00\x01'             # principal component count
c41359
-record += '\x00\x0bKRBTEST.COM' # realm
c41359
-record += '\x00\x04user'        # principal component
c41359
-record += '\x00\x00\x00\x01'    # name type (NT-PRINCIPAL)
c41359
-record += '\x54\xf7\x4d\x35'    # timestamp
c41359
-record += '\x02'                # key version
c41359
-record += '\x00\x12'            # enctype
c41359
-record += '\x00\x20'            # key length
c41359
-record += '\x00' * 32           # key bytes
c41359
-f = open(realm.keytab, 'w')
c41359
-f.write('\x05\x02\x00\x00\x00' + chr(len(record)))
c41359
+record = b'\x00\x01'             # principal component count
c41359
+record += b'\x00\x0bKRBTEST.COM' # realm
c41359
+record += b'\x00\x04user'        # principal component
c41359
+record += b'\x00\x00\x00\x01'    # name type (NT-PRINCIPAL)
c41359
+record += b'\x54\xf7\x4d\x35'    # timestamp
c41359
+record += b'\x02'                # key version
c41359
+record += b'\x00\x12'            # enctype
c41359
+record += b'\x00\x20'            # key length
c41359
+record += b'\x00' * 32           # key bytes
c41359
+f = open(realm.keytab, 'wb')
c41359
+f.write(b'\x05\x02\x00\x00\x00' + bytes([len(record)]))
c41359
 f.write(record)
c41359
 f.close()
c41359
 msg = '   2 %s' % realm.user_princ
c41359
 out = realm.run([klist, '-k'], expected_msg=msg)
c41359
 
c41359
 # Make sure zero-fill isn't treated as a 32-bit kvno.
c41359
-f = open(realm.keytab, 'w')
c41359
-f.write('\x05\x02\x00\x00\x00' + chr(len(record) + 4))
c41359
+f = open(realm.keytab, 'wb')
c41359
+f.write(b'\x05\x02\x00\x00\x00' + bytes([len(record) + 4]))
c41359
 f.write(record)
c41359
-f.write('\x00\x00\x00\x00')
c41359
+f.write(b'\x00\x00\x00\x00')
c41359
 f.close()
c41359
 msg = '   2 %s' % realm.user_princ
c41359
 out = realm.run([klist, '-k'], expected_msg=msg)
c41359
 
c41359
 # Make sure a hand-crafted 32-bit kvno is recognized.
c41359
-f = open(realm.keytab, 'w')
c41359
-f.write('\x05\x02\x00\x00\x00' + chr(len(record) + 4))
c41359
+f = open(realm.keytab, 'wb')
c41359
+f.write(b'\x05\x02\x00\x00\x00' + bytes([len(record) + 4]))
c41359
 f.write(record)
c41359
-f.write('\x00\x00\x00\x03')
c41359
+f.write(b'\x00\x00\x00\x03')
c41359
 f.close()
c41359
 msg = '   3 %s' % realm.user_princ
c41359
 out = realm.run([klist, '-k'], expected_msg=msg)
c41359
diff --git a/src/tests/t_mkey.py b/src/tests/t_mkey.py
c41359
index 48a533059..cbc830235 100755
c41359
--- a/src/tests/t_mkey.py
c41359
+++ b/src/tests/t_mkey.py
c41359
@@ -296,10 +296,10 @@ realm.stop()
c41359
 # 2. list_mkeys displays the same list as for a post-1.7 KDB.
c41359
 dumpfile = os.path.join(srctop, 'tests', 'dumpfiles', 'dump.16')
c41359
 os.remove(stash_file)
c41359
-f = open(stash_file, 'w')
c41359
+f = open(stash_file, 'wb')
c41359
 f.write(struct.pack('=HL24s', 16, 24,
c41359
-                    '\xF8\x3E\xFB\xBA\x6D\x80\xD9\x54\xE5\x5D\xF2\xE0'
c41359
-                    '\x94\xAD\x6D\x86\xB5\x16\x37\xEC\x7C\x8A\xBC\x86'))
c41359
+                    b'\xF8\x3E\xFB\xBA\x6D\x80\xD9\x54\xE5\x5D\xF2\xE0'
c41359
+                    b'\x94\xAD\x6D\x86\xB5\x16\x37\xEC\x7C\x8A\xBC\x86'))
c41359
 f.close()
c41359
 realm.run([kdb5_util, 'load', dumpfile])
c41359
 nprincs = len(realm.run([kadminl, 'listprincs']).splitlines())
c41359
diff --git a/src/tests/t_otp.py b/src/tests/t_otp.py
c41359
index 0fd35d576..617a8ecf5 100755
c41359
--- a/src/tests/t_otp.py
c41359
+++ b/src/tests/t_otp.py
c41359
@@ -29,8 +29,8 @@
c41359
 #
c41359
 
c41359
 from k5test import *
c41359
-from Queue import Empty
c41359
-import StringIO
c41359
+from queue import Empty
c41359
+from io import StringIO
c41359
 import struct
c41359
 
c41359
 try:
c41359
@@ -120,7 +120,8 @@ class UnixRadiusDaemon(RadiusDaemon):
c41359
         sock.listen(1)
c41359
         return (sock, addr)
c41359
 
c41359
-    def recvRequest(self, (sock, addr)):
c41359
+    def recvRequest(self, sock_and_addr):
c41359
+        sock, addr = sock_and_addr
c41359
         conn = sock.accept()[0]
c41359
         sock.close()
c41359
         os.remove(addr)
c41359
diff --git a/src/tests/t_tabdump.py b/src/tests/t_tabdump.py
c41359
index 2a86136dd..49531bf49 100755
c41359
--- a/src/tests/t_tabdump.py
c41359
+++ b/src/tests/t_tabdump.py
c41359
@@ -1,10 +1,10 @@
c41359
 from k5test import *
c41359
 
c41359
 import csv
c41359
-import StringIO
c41359
+from io import StringIO
c41359
 
c41359
 def tab_csv(s):
c41359
-    io = StringIO.StringIO(s)
c41359
+    io = StringIO(s)
c41359
     return list(csv.DictReader(io, dialect=csv.excel_tab))
c41359
 
c41359
 
c41359
diff --git a/src/util/Makefile.in b/src/util/Makefile.in
c41359
index 2611581c1..19a6bd312 100644
c41359
--- a/src/util/Makefile.in
c41359
+++ b/src/util/Makefile.in
c41359
@@ -26,3 +26,4 @@ install:
c41359
 
c41359
 clean-unix::
c41359
 	$(RM) *.pyc
c41359
+	$(RM) -r __pycache__
c41359
diff --git a/src/util/k5test.py b/src/util/k5test.py
c41359
index bc32877a7..81fac3063 100644
c41359
--- a/src/util/k5test.py
c41359
+++ b/src/util/k5test.py
c41359
@@ -380,16 +380,16 @@ import imp
c41359
 def fail(msg):
c41359
     """Print a message and exit with failure."""
c41359
     global _current_pass
c41359
-    print "*** Failure:", msg
c41359
+    print("*** Failure:", msg)
c41359
     if _last_mark:
c41359
-        print "*** Last mark: %s" % _last_mark
c41359
+        print("*** Last mark: %s" % _last_mark)
c41359
     if _last_cmd:
c41359
-        print "*** Last command (#%d): %s" % (_cmd_index - 1, _last_cmd)
c41359
+        print("*** Last command (#%d): %s" % (_cmd_index - 1, _last_cmd))
c41359
     if _last_cmd_output:
c41359
-        print "*** Output of last command:"
c41359
+        print("*** Output of last command:")
c41359
         sys.stdout.write(_last_cmd_output)
c41359
     if _current_pass:
c41359
-        print "*** Failed in test pass:", _current_pass
c41359
+        print("*** Failed in test pass:", _current_pass)
c41359
     sys.exit(1)
c41359
 
c41359
 
c41359
@@ -465,15 +465,16 @@ def _onexit():
c41359
         if not verbose:
c41359
             testlogfile = os.path.join(os.getcwd(), 'testlog')
c41359
             utildir = os.path.join(srctop, 'util')
c41359
-            print 'For details, see: %s' % testlogfile
c41359
-            print 'Or re-run this test script with the -v flag:'
c41359
-            print '    cd %s' % os.getcwd()
c41359
-            print '    PYTHONPATH=%s %s %s -v' % \
c41359
-                (utildir, sys.executable, sys.argv[0])
c41359
-            print
c41359
-        print 'Use --debug=NUM to run a command under a debugger.  Use'
c41359
-        print '--stop-after=NUM to stop after a daemon is started in order to'
c41359
-        print 'attach to it with a debugger.  Use --help to see other options.'
c41359
+            print('For details, see: %s' % testlogfile)
c41359
+            print('Or re-run this test script with the -v flag:')
c41359
+            print('    cd %s' % os.getcwd())
c41359
+            print('    PYTHONPATH=%s %s %s -v' %
c41359
+                  (utildir, sys.executable, sys.argv[0]))
c41359
+            print()
c41359
+        print('Use --debug=NUM to run a command under a debugger.  Use')
c41359
+        print('--stop-after=NUM to stop after a daemon is started in order to')
c41359
+        print('attach to it with a debugger.  Use --help to see other')
c41359
+        print('options.')
c41359
 
c41359
 
c41359
 def _onsigint(signum, frame):
c41359
@@ -523,8 +524,8 @@ def _get_hostname():
c41359
     hostname = socket.gethostname()
c41359
     try:
c41359
         ai = socket.getaddrinfo(hostname, None, 0, 0, 0, socket.AI_CANONNAME)
c41359
-    except socket.gaierror, (error, errstr):
c41359
-        fail('Local hostname "%s" does not resolve: %s.' % (hostname, errstr))
c41359
+    except socket.gaierror as e:
c41359
+        fail('Local hostname "%s" does not resolve: %s.' % (hostname, e[1]))
c41359
     (family, socktype, proto, canonname, sockaddr) = ai[0]
c41359
     try:
c41359
         name = socket.getnameinfo(sockaddr, socket.NI_NAMEREQD)
c41359
@@ -594,7 +595,7 @@ def _match_cmdnum(cmdnum, ind):
c41359
 def _build_env():
c41359
     global buildtop, runenv
c41359
     env = os.environ.copy()
c41359
-    for (k, v) in runenv.env.iteritems():
c41359
+    for (k, v) in runenv.env.items():
c41359
         if v.find('./') == 0:
c41359
             env[k] = os.path.join(buildtop, v)
c41359
         else:
c41359
@@ -704,7 +705,8 @@ def _run_cmd(args, env, input=None, expected_code=0, expected_msg=None,
c41359
 
c41359
     # Run the command and log the result, folding stderr into stdout.
c41359
     proc = subprocess.Popen(args, stdin=infile, stdout=subprocess.PIPE,
c41359
-                            stderr=subprocess.STDOUT, env=env)
c41359
+                            stderr=subprocess.STDOUT, env=env,
c41359
+                            universal_newlines=True)
c41359
     (outdata, dummy_errdata) = proc.communicate(input)
c41359
     _last_cmd_output = outdata
c41359
     code = proc.returncode
c41359
@@ -734,10 +736,10 @@ def _debug_cmd(args, env, input):
c41359
            (_cmd_index, _shell_equiv(args)), True)
c41359
     if input:
c41359
         print
c41359
-        print '*** Enter the following input when appropriate:'
c41359
-        print 
c41359
-        print input
c41359
-        print
c41359
+        print('*** Enter the following input when appropriate:')
c41359
+        print()
c41359
+        print(input)
c41359
+        print()
c41359
     code = subprocess.call(args, env=env)
c41359
     output('*** [%d] Completed in debugger with return code %d\n' %
c41359
            (_cmd_index, code))
c41359
@@ -765,7 +767,8 @@ def _start_daemon(args, env, sentinel):
c41359
 
c41359
     # Start the daemon and look for the sentinel in stdout or stderr.
c41359
     proc = subprocess.Popen(args, stdin=null_input, stdout=subprocess.PIPE,
c41359
-                            stderr=subprocess.STDOUT, env=env)
c41359
+                            stderr=subprocess.STDOUT, env=env,
c41359
+                            universal_newlines=True)
c41359
     _last_cmd_output = ''
c41359
     while True:
c41359
         line = proc.stdout.readline()
c41359
diff --git a/src/util/princflags.py b/src/util/princflags.py
c41359
index f568dd2f1..f645e86e4 100644
c41359
--- a/src/util/princflags.py
c41359
+++ b/src/util/princflags.py
c41359
@@ -1,5 +1,4 @@
c41359
 import re
c41359
-import string
c41359
 
c41359
 # Module for translating KDB principal flags between string and
c41359
 # integer forms.
c41359
@@ -81,7 +80,7 @@ _prefixlen = len(_prefix)
c41359
 _flagnames = {}
c41359
 
c41359
 # Translation table to map hyphens to underscores
c41359
-_squash = string.maketrans('-', '_')
c41359
+_squash = str.maketrans('-', '_')
c41359
 
c41359
 # Combined input-to-flag lookup table, to be filled in by
c41359
 # _setup_tables()
c41359
@@ -176,7 +175,7 @@ def flagnum2str(n):
c41359
 # Return a list of flag names from a flag word.
c41359
 def flags2namelist(flags):
c41359
     a = []
c41359
-    for n in xrange(32):
c41359
+    for n in range(32):
c41359
         if flags & (1 << n):
c41359
             a.append(flagnum2str(n))
c41359
     return a
c41359
@@ -225,21 +224,21 @@ def speclist2mask(s):
c41359
 
c41359
 # Print C table of input flag specifiers for lib/kadm5/str_conv.c.
c41359
 def _print_ftbl():
c41359
-    print 'static const struct flag_table_row ftbl[] = {'
c41359
-    a = sorted(pflags.items(), key=lambda (k, v): (v.flag, -v.invert, k))
c41359
+    print('static const struct flag_table_row ftbl[] = {')
c41359
+    a = sorted(pflags.items(), key=lambda k, v: (v.flag, -v.invert, k))
c41359
     for k, v in a:
c41359
         s1 = '    {"%s",' % k
c41359
         s2 = '%-31s KRB5_KDB_%s,' % (s1, v.flagname())
c41359
-        print '%-63s %d},' % (s2, 1 if v.invert else 0)
c41359
+        print('%-63s %d},' % (s2, 1 if v.invert else 0))
c41359
 
c41359
-    print '};'
c41359
-    print '#define NFTBL (sizeof(ftbl) / sizeof(ftbl[0]))'
c41359
+    print('};')
c41359
+    print('#define NFTBL (sizeof(ftbl) / sizeof(ftbl[0]))')
c41359
 
c41359
 
c41359
 # Print C table of output flag names for lib/kadm5/str_conv.c.
c41359
 def _print_outflags():
c41359
-    print 'static const char *outflags[] = {'
c41359
-    for i in xrange(32):
c41359
+    print('static const char *outflags[] = {')
c41359
+    for i in range(32):
c41359
         flag = 1 << i
c41359
         if flag > max(_flagnames.keys()):
c41359
             break
c41359
@@ -247,10 +246,10 @@ def _print_outflags():
c41359
             s = '    "%s",' % _flagnames[flag]
c41359
         except KeyError:
c41359
             s = '    NULL,'
c41359
-        print '%-32s/* 0x%08x */' % (s, flag)
c41359
+        print('%-32s/* 0x%08x */' % (s, flag))
c41359
 
c41359
-    print '};'
c41359
-    print '#define NOUTFLAGS (sizeof(outflags) / sizeof(outflags[0]))'
c41359
+    print('};')
c41359
+    print('#define NOUTFLAGS (sizeof(outflags) / sizeof(outflags[0]))')
c41359
 
c41359
 
c41359
 # Print out C tables to insert into lib/kadm5/str_conv.c.