Issue 2327: provide a compat sorted() function for pre-2.4 Python versions.
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Thu, 25 Mar 2010 17:45:47 +0000 (17:45 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Thu, 25 Mar 2010 17:45:47 +0000 (17:45 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@4732 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/engine/SCons/BuilderTests.py
src/engine/SCons/EnvironmentTests.py
src/engine/SCons/SubstTests.py
src/engine/SCons/compat/builtins.py

index d14f777f2e99db8222ad473d70d19f8b0080c5ad..f26aa6773f467b153be8f7b260ece6ec59a57e55 100644 (file)
@@ -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
 
 
index 7d3d33eaa246157cac307f7f65ef4c5472676b78..b692e96ff41090e99b6ee7d1bf335bf7c2503a37 100644 (file)
@@ -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):
index 6ef15cf2692376a78b669d2ef1599cd1701078b7..b587dfae029902d8a92902578151703c5feec2ac 100644 (file)
@@ -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__":
index d52ea4deacc2edcf9d9abab1a0eeab2909d687d4..064415ecfa5fab16bf478e053123a1cb08bd9d19 100644 (file)
@@ -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