From 00a3188193ba1feef927cf18e7f5fc20ad71b848 Mon Sep 17 00:00:00 2001 From: Steven Knight Date: Thu, 25 Mar 2010 17:45:47 +0000 Subject: Issue 2327: provide a compat sorted() function for pre-2.4 Python versions. --- src/engine/SCons/BuilderTests.py | 14 ++++++-------- src/engine/SCons/EnvironmentTests.py | 19 +++++++------------ src/engine/SCons/SubstTests.py | 14 ++++++-------- src/engine/SCons/compat/builtins.py | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src/engine/SCons/BuilderTests.py b/src/engine/SCons/BuilderTests.py index d14f777..f26aa67 100644 --- a/src/engine/SCons/BuilderTests.py +++ b/src/engine/SCons/BuilderTests.py @@ -23,6 +23,8 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" +import SCons.compat + # Define a null function for use as a builder action. # Where this is defined in the file seems to affect its # byte-code contents, so try to minimize changes by @@ -566,13 +568,11 @@ class BuilderTestCase(unittest.TestCase): "Unexpected tgt.sources[0] name: %s" % tgt.sources[0].path b2 = SCons.Builder.Builder(src_suffix = '.2', src_builder = b1) - r = b2.src_suffixes(env) - r.sort() + r = sorted(b2.src_suffixes(env)) assert r == ['.2', '.c'], r b3 = SCons.Builder.Builder(action = {'.3a' : '', '.3b' : ''}) - s = b3.src_suffixes(env) - s.sort() + s = sorted(b3.src_suffixes(env)) assert s == ['.3a', '.3b'], s b4 = SCons.Builder.Builder(src_suffix = '$XSUFFIX') @@ -1032,8 +1032,7 @@ class BuilderTestCase(unittest.TestCase): bld.set_src_suffix(['.bar', '.foo']) r = bld.get_src_suffix(env) assert r == '.bar', r - r = bld.src_suffixes(env) - r.sort() + r = sorted(bld.src_suffixes(env)) assert r == ['.bar', '.foo'], r # adjust_suffix normalizes the suffix, adding a `.' if needed @@ -1153,8 +1152,7 @@ class BuilderTestCase(unittest.TestCase): assert r is None, r r = builder.get_src_suffix(env) assert r == '.src_sfx1', r - r = builder.src_suffixes(env) - r.sort() + r = sorted(builder.src_suffixes(env)) assert r == ['.src_sfx1', '.src_sfx2'], r diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index 7d3d33e..b692e96 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -23,6 +23,8 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" +import SCons.compat + import copy import os import StringIO @@ -40,9 +42,7 @@ def diff_env(env1, env2): d = {} for k in env1._dict.keys() + env2._dict.keys(): d[k] = None - keys = d.keys() - keys.sort() - for k in keys: + for k in sorted(d.keys()): if k in env1: if k in env2: if env1[k] != env2[k]: @@ -62,9 +62,7 @@ def diff_dict(d1, d2): d = {} for k in d1.keys() + d2.keys(): d[k] = None - keys = d.keys() - keys.sort() - for k in keys: + for k in sorted(d.keys()): if k in d1: if k in d2: if d1[k] != d2[k]: @@ -3667,14 +3665,11 @@ class OverrideEnvironmentTestCase(unittest.TestCase,TestEnvironmentFixture): def test_items(self): """Test the OverrideEnvironment items() method""" env, env2, env3 = self.envs - items = env.items() - items.sort() + items = sorted(env.items()) assert items == [('XXX', 'x'), ('YYY', 'y')], items - items = env2.items() - items.sort() + items = sorted(env2.items()) assert items == [('XXX', 'x2'), ('YYY', 'y')], items - items = env3.items() - items.sort() + items = sorted(env3.items()) assert items == [('XXX', 'x3'), ('YYY', 'y3'), ('ZZZ', 'z3')], items def test_gvars(self): diff --git a/src/engine/SCons/SubstTests.py b/src/engine/SCons/SubstTests.py index 6ef15cf..b587dfa 100644 --- a/src/engine/SCons/SubstTests.py +++ b/src/engine/SCons/SubstTests.py @@ -23,6 +23,8 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" +import SCons.compat + import os import os.path import StringIO @@ -1180,12 +1182,10 @@ class subst_dict_TestCase(unittest.TestCase): s1 = DummyNode('s1') s2 = DummyNode('s2') d = subst_dict(target=[t1, t2], source=[s1, s2]) - TARGETS = [str(x) for x in d['TARGETS']] - TARGETS.sort() + TARGETS = sorted([str(x) for x in d['TARGETS']]) assert TARGETS == ['t1', 't2'], d['TARGETS'] assert str(d['TARGET']) == 't1', d['TARGET'] - SOURCES = [str(x) for x in d['SOURCES']] - SOURCES.sort() + SOURCES = sorted([str(x) for x in d['SOURCES']]) assert SOURCES == ['s1', 's2'], d['SOURCES'] assert str(d['SOURCE']) == 's1', d['SOURCE'] @@ -1209,11 +1209,9 @@ class subst_dict_TestCase(unittest.TestCase): s4 = N('s4') s5 = V('s5') d = subst_dict(target=[t3, t4, t5], source=[s3, s4, s5]) - TARGETS = [str(x) for x in d['TARGETS']] - TARGETS.sort() + TARGETS = sorted([str(x) for x in d['TARGETS']]) assert TARGETS == ['t4', 'v-t3', 'v-t5'], TARGETS - SOURCES = [str(x) for x in d['SOURCES']] - SOURCES.sort() + SOURCES = sorted([str(x) for x in d['SOURCES']]) assert SOURCES == ['s3', 'v-rstr-s4', 'v-s5'], SOURCES if __name__ == "__main__": diff --git a/src/engine/SCons/compat/builtins.py b/src/engine/SCons/compat/builtins.py index d52ea4d..064415e 100644 --- a/src/engine/SCons/compat/builtins.py +++ b/src/engine/SCons/compat/builtins.py @@ -39,6 +39,7 @@ This module checks for the following __builtin__ names: any() bool() dict() + sorted() True False zip() @@ -141,6 +142,38 @@ except NameError: # Pre-2.2 Python has no file() function. __builtin__.file = open +try: + sorted +except NameError: + # Pre-2.4 Python has no sorted() function. + # + # The pre-2.4 Python list.sort() method does not support + # list.sort(key=) nor list.sort(reverse=) keyword arguments, so + # we must implement the functionality of those keyword arguments + # by hand instead of passing them to list.sort(). + def sorted(iterable, cmp=None, key=None, reverse=False): + if key: + decorated = [ (key(x), x) for x in iterable ] + if cmp is None: + # Pre-2.3 Python does not support list.sort(None). + decorated.sort() + else: + decorated.sort(cmp) + if reverse: + decorated.reverse() + result = [ t[1] for t in decorated ] + else: + result = iterable[:] + if cmp is None: + # Pre-2.3 Python does not support list.sort(None). + result.sort() + else: + result.sort(cmp) + if reverse: + result.reverse() + return result + __builtin__.sorted = sorted + # try: zip -- cgit v0.12