From a136fc8307d03ac0bb728163da10de18c7285104 Mon Sep 17 00:00:00 2001 From: Gary Oberbrunner Date: Fri, 5 Dec 2008 03:22:28 +0000 Subject: Fix issue #3: make Append/PrependUnique uniquify the appended/prepended list first. --- src/engine/SCons/Environment.py | 22 ++++++++++++++++++++++ src/engine/SCons/EnvironmentTests.py | 12 ++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index 0873354..5b82086 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -142,6 +142,24 @@ def _set_SCANNERS(env, key, value): env._dict[key] = value env.scanner_map_delete() +def _delete_duplicates(l, keep_last): + """Delete duplicates from a sequence, keeping the first or last.""" + seen={} + result=[] + if keep_last: # reverse in & out, then keep first + l.reverse() + for i in l: + try: + if not seen.has_key(i): + result.append(i) + seen[i]=1 + except TypeError: + # probably unhashable. Just keep it. + result.append(i) + if keep_last: + result.reverse() + return result + # The following is partly based on code in a comment added by Peter @@ -1193,6 +1211,8 @@ class Base(SubstitutionEnvironment): """ kw = copy_non_reserved_keywords(kw) for key, val in kw.items(): + if SCons.Util.is_List(val): + val = _delete_duplicates(val, delete_existing) if not self._dict.has_key(key) or self._dict[key] in ('', None): self._dict[key] = val elif SCons.Util.is_Dict(self._dict[key]) and \ @@ -1546,6 +1566,8 @@ class Base(SubstitutionEnvironment): """ kw = copy_non_reserved_keywords(kw) for key, val in kw.items(): + if SCons.Util.is_List(val): + val = _delete_duplicates(val, not delete_existing) if not self._dict.has_key(key) or self._dict[key] in ('', None): self._dict[key] = val elif SCons.Util.is_Dict(self._dict[key]) and \ diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index f738891..2c09378 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -1599,12 +1599,12 @@ def exists(env): DDD1 = ['a', 'b', 'c']) env.AppendUnique(AAA1 = 'a1', AAA2 = ['a2'], - AAA3 = ['a3', 'b', 'c', 'a3'], + AAA3 = ['a3', 'b', 'c', 'c', 'b', 'a3'], # ignore dups AAA4 = 'a4.new', AAA5 = ['a5.new'], BBB1 = 'b1', BBB2 = ['b2'], - BBB3 = ['b3', 'c', 'd', 'b3'], + BBB3 = ['b3', 'c', 'd', 'c', 'b3'], BBB4 = 'b4.new', BBB5 = ['b5.new'], CCC1 = 'c1', @@ -1629,6 +1629,8 @@ def exists(env): assert env['DDD1'] == ['a', 'c', 'b'], env['DDD1'] # b moves to end env.AppendUnique(DDD1 = ['a','b'], delete_existing=1) assert env['DDD1'] == ['c', 'a', 'b'], env['DDD1'] # a & b move to end + env.AppendUnique(DDD1 = ['e','f', 'e'], delete_existing=1) + assert env['DDD1'] == ['c', 'a', 'b', 'f', 'e'], env['DDD1'] # add last env['CLVar'] = CLVar([]) env.AppendUnique(CLVar = 'bar') @@ -2255,7 +2257,7 @@ f5: \ DDD1 = ['a', 'b', 'c']) env.PrependUnique(AAA1 = 'a1', AAA2 = ['a2'], - AAA3 = ['a3', 'b', 'c', 'a3'], + AAA3 = ['a3', 'b', 'c', 'b', 'a3'], # ignore dups AAA4 = 'a4.new', AAA5 = ['a5.new'], BBB1 = 'b1', @@ -2268,7 +2270,7 @@ f5: \ DDD1 = 'b') assert env['AAA1'] == 'a1a1', env['AAA1'] assert env['AAA2'] == ['a2'], env['AAA2'] - assert env['AAA3'] == ['b', 'c', 'a3'], env['AAA3'] + assert env['AAA3'] == ['c', 'b', 'a3'], env['AAA3'] assert env['AAA4'] == 'a4.newa4', env['AAA4'] assert env['AAA5'] == ['a5.new', 'a5'], env['AAA5'] assert env['BBB1'] == ['b1'], env['BBB1'] @@ -2284,6 +2286,8 @@ f5: \ assert env['DDD1'] == ['b', 'a', 'c'], env['DDD1'] # b moves to front env.PrependUnique(DDD1 = ['a','c'], delete_existing=1) assert env['DDD1'] == ['a', 'c', 'b'], env['DDD1'] # a & c move to front + env.PrependUnique(DDD1 = ['d','e','d'], delete_existing=1) + assert env['DDD1'] == ['d', 'e', 'a', 'c', 'b'], env['DDD1'] env['CLVar'] = CLVar([]) -- cgit v0.12