From c2cf24a5be13852cc65c4a9071f52f2715bc409c Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Wed, 29 May 2019 07:49:22 -0600 Subject: Tweak a few tests "for i in range(len(foo))" idiom changed to iterate directly over lists instead of indexing them. zip() generates the iterator in the case with two lists. reversed() used for the Windows drive-letter test. gdbm is not gone, just renamed. change test to find under either name. Use a context manager for closing StringIO objects opened for capturing standard I/O streams. Signed-off-by: Mats Wichmann --- test/Batch/action-changed.py | 10 ++++------ test/SConsignFile/use-gdbm.py | 13 +++++++++---- test/SWIG/build-dir.py | 4 ++-- test/Win32/bad-drive.py | 4 ++-- test/option--C.py | 8 ++++---- test/option/option_profile.py | 41 +++++++++++++++++++---------------------- 6 files changed, 40 insertions(+), 40 deletions(-) diff --git a/test/Batch/action-changed.py b/test/Batch/action-changed.py index 777c7fb..b2b1673 100644 --- a/test/Batch/action-changed.py +++ b/test/Batch/action-changed.py @@ -43,12 +43,10 @@ import sys sep = sys.argv.index('--') targets = sys.argv[1:sep] sources = sys.argv[sep+1:] -for i in range(len(targets)): - t = targets[i] - s = sources[i] - with open(t, 'wb') as fp, open(s, 'rb') as infp: - fp.write(bytearray('%s\\n','utf-8')) - fp.write(infp.read()) +for t, s in zip(targets, sources): + with open(t, 'wb') as ofp, open(s, 'rb') as ifp: + ofp.write(bytearray('%s\\n', 'utf-8')) + ofp.write(ifp.read()) sys.exit(0) """ diff --git a/test/SConsignFile/use-gdbm.py b/test/SConsignFile/use-gdbm.py index 8f863fb..12a2e8b 100644 --- a/test/SConsignFile/use-gdbm.py +++ b/test/SConsignFile/use-gdbm.py @@ -36,8 +36,13 @@ test = TestSCons.TestSCons() try: import gdbm + use_dbm = "gdbm" except ImportError: - test.skip_test('No gdbm in this version of Python; skipping test.\n') + try: + import dbm.gnu + use_dbm = "dbm.gnu" + except ImportError: + test.skip_test('No GNU dbm in this version of Python; skipping test.\n') test.subdir('subdir') @@ -51,8 +56,8 @@ sys.exit(0) # test.write('SConstruct', """ import sys -import gdbm -SConsignFile('.sconsign', gdbm) +import %(use_dbm)s +SConsignFile('.sconsign', %(use_dbm)s) B = Builder(action = '%(_python_)s build.py $TARGETS $SOURCES') env = Environment(BUILDERS = { 'B' : B }) env.B(target = 'f1.out', source = 'f1.in') @@ -72,7 +77,7 @@ test.must_exist(test.workpath('.sconsign')) test.must_not_exist(test.workpath('.sconsign.dblite')) test.must_not_exist(test.workpath('subdir', '.sconsign')) test.must_not_exist(test.workpath('subdir', '.sconsign.dblite')) - + test.must_match('f1.out', "f1.in\n") test.must_match('f2.out', "f2.in\n") test.must_match(['subdir', 'f3.out'], "subdir/f3.in\n") diff --git a/test/SWIG/build-dir.py b/test/SWIG/build-dir.py index bc105a5..f70e6e1 100644 --- a/test/SWIG/build-dir.py +++ b/test/SWIG/build-dir.py @@ -132,8 +132,8 @@ public: %pythoncode %{ def __iter__(self): - for i in range(len(self)): - yield self[i] + for s in self: + yield s %} } }; diff --git a/test/Win32/bad-drive.py b/test/Win32/bad-drive.py index 424732d..78f5395 100644 --- a/test/Win32/bad-drive.py +++ b/test/Win32/bad-drive.py @@ -43,9 +43,9 @@ if sys.platform != 'win32': msg = "Skipping drive-letter test on non-Windows platform '%s'\n" % sys.platform test.skip_test(msg) +# start at the end looking for unused drive letter bad_drive = None -for i in range(len(ascii_uppercase)-1, -1, -1): - d = ascii_uppercase[i] +for d in reversed(ascii_uppercase): if not os.path.isdir(d + ':' + os.sep): bad_drive = d + ':' break diff --git a/test/option--C.py b/test/option--C.py index 754d4db..3802bce 100644 --- a/test/option--C.py +++ b/test/option--C.py @@ -34,10 +34,10 @@ def match_normcase(lines, matches): if not isinstance(matches, list): matches = matches.split("\n") if len(lines) != len(matches): - return - for i in range(len(lines)): - if os.path.normcase(lines[i]) != os.path.normcase(matches[i]): - return + return None + for line, match in zip(lines, matches): + if os.path.normcase(line) != os.path.normcase(match): + return None return 1 test = TestSCons.TestSCons(match=match_normcase) diff --git a/test/option/option_profile.py b/test/option/option_profile.py index 4538e0e..c252b39 100644 --- a/test/option/option_profile.py +++ b/test/option/option_profile.py @@ -26,17 +26,16 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" import sys -if sys.version_info[0] < 3: - import io - _StringIO = io.StringIO - # TODO(2.6): In 2.6 and beyond, the io.StringIO.write() method - # requires unicode strings. This subclass can probably be removed - # when we drop support for Python 2.6. - class StringIO(_StringIO): - def write(self, s): - _StringIO.write(self, unicode(s)) -else: +# TODO: Fixup StringIO usage when Py2.7 is dropped. +# cheat a little bit: io.StringIO is "preferred" in Py2.7 +# since it forces you to be explicit about strings (it is unicode-only) +# It's easier to use the unaware version. Which also doesn't come +# with a context manager, so use contextlib.closing +try: + from cStringIO import StringIO +except ImportError: from io import StringIO +import contextlib import TestSCons @@ -61,14 +60,13 @@ test.must_contain_all_lines(test.stdout(), ['usage: scons [OPTION]']) try: save_stdout = sys.stdout - sys.stdout = StringIO() + with contextlib.closing(StringIO()) as sys.stdout: + stats = pstats.Stats(scons_prof) + stats.sort_stats('time') - stats = pstats.Stats(scons_prof) - stats.sort_stats('time') + stats.strip_dirs().print_stats() - stats.strip_dirs().print_stats() - - s = sys.stdout.getvalue() + s = sys.stdout.getvalue() finally: sys.stdout = save_stdout @@ -82,14 +80,13 @@ test.run(arguments = "--profile %s" % scons_prof) try: save_stdout = sys.stdout - sys.stdout = StringIO() - - stats = pstats.Stats(scons_prof) - stats.sort_stats('time') + with contextlib.closing(StringIO()) as sys.stdout: + stats = pstats.Stats(scons_prof) + stats.sort_stats('time') - stats.strip_dirs().print_stats() + stats.strip_dirs().print_stats() - s = sys.stdout.getvalue() + s = sys.stdout.getvalue() finally: sys.stdout = save_stdout -- cgit v0.12