From: cclauss Date: Thu, 15 Nov 2018 19:17:33 +0000 (+0100) Subject: Fix some Python 3 incompatibilities X-Git-Tag: auth-4.2.0-alpha1~38^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F7202%2Fhead;p=thirdparty%2Fpdns.git Fix some Python 3 incompatibilities --- diff --git a/regression-tests.api/runtests.py b/regression-tests.api/runtests.py index 45145a6181..9263fcf19b 100755 --- a/regression-tests.api/runtests.py +++ b/regression-tests.api/runtests.py @@ -11,6 +11,11 @@ import sys import tempfile import time +try: + raw_input +except NameError: + raw_input = input + SQLITE_DB = 'pdns.sqlite3' WEBPORT = 5556 DNSPORT = 5300 diff --git a/regression-tests.api/test_helper.py b/regression-tests.api/test_helper.py index 2affa83a4b..06a3e5dd04 100644 --- a/regression-tests.api/test_helper.py +++ b/regression-tests.api/test_helper.py @@ -84,7 +84,7 @@ def pdnsutil(subcommand, *args): try: return subprocess.check_output(PDNSUTIL_CMD + [subcommand] + list(args), close_fds=True).decode('ascii') except subprocess.CalledProcessError as except_inst: - raise RuntimeError("pdnsutil %s %s failed: %s" % (command, args, except_inst.output.decode('ascii', errors='replace'))) + raise RuntimeError("pdnsutil %s %s failed: %s" % (subcommand, args, except_inst.output.decode('ascii', errors='replace'))) def pdnsutil_rectify(zonename): """Run pdnsutil rectify-zone on the given zone.""" diff --git a/regression-tests.auth-py/authtests.py b/regression-tests.auth-py/authtests.py index 732ad27111..c680a82dc3 100644 --- a/regression-tests.auth-py/authtests.py +++ b/regression-tests.auth-py/authtests.py @@ -1,5 +1,6 @@ #!/usr/bin/env python2 +from __future__ import print_function import errno import shutil import os @@ -64,7 +65,7 @@ PrivateKey: Lt0v0Gol3pRUFM7fDdcy0IWN0O/MnEmVPA+VylL8Y4U= except OSError as e: if e.errno != errno.ENOENT: raise - os.mkdir(confdir, 0755) + os.mkdir(confdir, 0o755) @classmethod def generateAuthZone(cls, confdir, zonename, zonecontent): @@ -119,11 +120,11 @@ distributor-threads=1""".format(confdir=confdir, prefix=cls._PREFIX, 'create-bind-db', bind_dnssec_db] - print ' '.join(pdnsutilCmd) + print(' '.join(pdnsutilCmd)) try: subprocess.check_output(pdnsutilCmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: - print e.output + print(e.output) raise @classmethod @@ -147,11 +148,11 @@ distributor-threads=1""".format(confdir=confdir, prefix=cls._PREFIX, 'active', 'ksk'] - print ' '.join(pdnsutilCmd) + print(' '.join(pdnsutilCmd)) try: subprocess.check_output(pdnsutilCmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: - print e.output + print(e.output) raise @classmethod @@ -194,7 +195,7 @@ distributor-threads=1""".format(confdir=confdir, prefix=cls._PREFIX, if e.errno != errno.ESRCH: raise with open(logFile, 'r') as fdLog: - print fdLog.read() + print(fdLog.read()) sys.exit(cls._auths[ipaddress].returncode) @classmethod diff --git a/regression-tests.dnsdist/dnsdisttests.py b/regression-tests.dnsdist/dnsdisttests.py index c02afb8437..91fc086b49 100644 --- a/regression-tests.dnsdist/dnsdisttests.py +++ b/regression-tests.dnsdist/dnsdisttests.py @@ -17,12 +17,15 @@ import libnacl import libnacl.utils # Python2/3 compatibility hacks -if sys.version_info[0] == 2: +try: + from queue import Queue +except ImportError: from Queue import Queue + +try: range = xrange -else: - from queue import Queue - range = range # allow re-export of the builtin name +except NameError: + pass class DNSDistTest(unittest.TestCase): diff --git a/regression-tests.recursor-dnssec/printlogs.py b/regression-tests.recursor-dnssec/printlogs.py index 1267ba5f2c..9dcbdeedd9 100755 --- a/regression-tests.recursor-dnssec/printlogs.py +++ b/regression-tests.recursor-dnssec/printlogs.py @@ -1,5 +1,6 @@ #!/usr/bin/env python2 +from __future__ import print_function import xml.etree.ElementTree import os.path import glob @@ -15,13 +16,13 @@ for child in root: confdir = os.path.join("configs", confdirname) recursorlog = os.path.join(confdir, "recursor.log") if os.path.exists(recursorlog): - print "==============> %s <==============" % recursorlog + print("==============> %s <==============" % recursorlog) with open(recursorlog) as f: - print ''.join(f.readlines()) + print(''.join(f.readlines())) authdirs = glob.glob(os.path.join(confdir, "auth-*")) for authdir in authdirs: authlog = os.path.join(authdir, "pdns.log") if os.path.exists(recursorlog): - print "==============> %s <==============" % authlog + print("==============> %s <==============" % authlog) with open(authlog) as f: - print ''.join(f.readlines()) + print(''.join(f.readlines())) diff --git a/regression-tests.recursor-dnssec/recursortests.py b/regression-tests.recursor-dnssec/recursortests.py index d2a286858f..ec1b0fce46 100644 --- a/regression-tests.recursor-dnssec/recursortests.py +++ b/regression-tests.recursor-dnssec/recursortests.py @@ -1,5 +1,6 @@ #!/usr/bin/env python2 +from __future__ import print_function import errno import shutil import os @@ -290,7 +291,7 @@ PrivateKey: kvoV/g4IO/tefSro+FLJ5UC7H3BUf0IUtZQSUOfQGyA= except OSError as e: if e.errno != errno.ENOENT: raise - os.mkdir(confdir, 0755) + os.mkdir(confdir, 0o755) @classmethod def generateAuthZone(cls, confdir, zonename, zonecontent): @@ -340,11 +341,11 @@ distributor-threads=1""".format(confdir=confdir, 'create-bind-db', bind_dnssec_db] - print ' '.join(pdnsutilCmd) + print(' '.join(pdnsutilCmd)) try: subprocess.check_output(pdnsutilCmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: - print e.output + print(e.output) raise @classmethod @@ -368,11 +369,11 @@ distributor-threads=1""".format(confdir=confdir, 'active', 'ksk'] - print ' '.join(pdnsutilCmd) + print(' '.join(pdnsutilCmd)) try: subprocess.check_output(pdnsutilCmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: - print e.output + print(e.output) raise @classmethod @@ -424,7 +425,7 @@ distributor-threads=1""".format(confdir=confdir, if e.errno != errno.ESRCH: raise with open(logFile, 'r') as fdLog: - print fdLog.read() + print(fdLog.read()) sys.exit(cls._auths[ipaddress].returncode) @classmethod @@ -492,7 +493,7 @@ distributor-threads=1""".format(confdir=confdir, if e.errno != errno.ESRCH: raise with open(logFile, 'r') as fdLog: - print fdLog.read() + print(fdLog.read()) sys.exit(cls._recursor.returncode) @classmethod @@ -504,7 +505,7 @@ distributor-threads=1""".format(confdir=confdir, try: subprocess.check_output(rec_controlCmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: - print e.output + print(e.output) raise @classmethod diff --git a/regression-tests.recursor-dnssec/test_Protobuf.py b/regression-tests.recursor-dnssec/test_Protobuf.py index 813087be02..50a6449d4b 100644 --- a/regression-tests.recursor-dnssec/test_Protobuf.py +++ b/regression-tests.recursor-dnssec/test_Protobuf.py @@ -8,12 +8,15 @@ import threading import time # Python2/3 compatibility hacks -if sys.version_info[0] == 2: +try: + from queue import Queue +except ImportError: from Queue import Queue + +try: range = xrange -else: - from queue import Queue - range = range # allow re-export of the builtin name +except NameError: + pass from recursortests import RecursorTest diff --git a/regression-tests.recursor-dnssec/test_basicNSEC3.py b/regression-tests.recursor-dnssec/test_basicNSEC3.py index 5185f5f227..870ba2e343 100644 --- a/regression-tests.recursor-dnssec/test_basicNSEC3.py +++ b/regression-tests.recursor-dnssec/test_basicNSEC3.py @@ -1,3 +1,4 @@ +from __future__ import print_function from basicDNSSEC import BasicDNSSEC import os import subprocess @@ -27,11 +28,11 @@ class basicNSEC3(BasicDNSSEC): 'active', 'ksk'] - print ' '.join(pdnsutilCmd) + print(' '.join(pdnsutilCmd)) try: subprocess.check_output(pdnsutilCmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: - print e.output + print(e.output) raise params = "1 0 100 AABBCCDDEEFF112233" @@ -45,9 +46,9 @@ class basicNSEC3(BasicDNSSEC): zone, params] - print ' '.join(pdnsutilCmd) + print(' '.join(pdnsutilCmd)) try: subprocess.check_output(pdnsutilCmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: - print e.output + print(e.output) raise diff --git a/regression-tests/bulktest-report.py b/regression-tests/bulktest-report.py index f756cca544..1340014db4 100755 --- a/regression-tests/bulktest-report.py +++ b/regression-tests/bulktest-report.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function import json, sys runs = json.load(sys.stdin) @@ -26,6 +27,6 @@ selected.sort() names.discard('tag') fmt=''.join('%%%ds' % max(15, i+4) for i in [3]+map(len, sorted(names))) -print fmt % tuple(['tag']+sorted(names)) +print(fmt % tuple(['tag']+sorted(names))) for tag, stats in selected: - print fmt % tuple([tag] + [stats.get(s) for s in sorted(names)]) + print(fmt % tuple([tag] + [stats.get(s) for s in sorted(names)])) diff --git a/regression-tests/bulktest-to-json.py b/regression-tests/bulktest-to-json.py index 1084d173c9..27a6b1454c 100755 --- a/regression-tests/bulktest-to-json.py +++ b/regression-tests/bulktest-to-json.py @@ -1,4 +1,5 @@ #!/usr/bin/env python2 +from __future__ import print_function import glob, json varnames = set() @@ -24,4 +25,4 @@ for fname in glob.glob('testresults-*.xml'): # print varnames # print statnames -print json.dumps(runs) +print(json.dumps(runs))