From 71b8a68a60f373e408a3cf91ec5b739efc8a5c66 Mon Sep 17 00:00:00 2001 From: stevenknight Date: Tue, 11 Dec 2001 06:18:49 +0000 Subject: [PATCH] Add a __hash_() method to the Scanners. git-svn-id: http://scons.tigris.org/svn/scons/trunk@141 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- src/engine/SCons/Scanner/C.py | 7 ++++++- src/engine/SCons/Scanner/CTests.py | 21 ++++++++++++++++----- src/engine/SCons/Scanner/ScannerTests.py | 10 ++++++++++ src/engine/SCons/Scanner/__init__.py | 12 ++++++++---- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/engine/SCons/Scanner/C.py b/src/engine/SCons/Scanner/C.py index 5dbdf6f6..589aef69 100644 --- a/src/engine/SCons/Scanner/C.py +++ b/src/engine/SCons/Scanner/C.py @@ -50,6 +50,7 @@ def CScan(fs = SCons.Node.FS.default_fs): class CScanner(SCons.Scanner.Recursive): def __init__(self, *args, **kw): apply(SCons.Scanner.Recursive.__init__, (self,) + args, kw) + self.hash = None self.pathscanners = {} def instance(self, env): @@ -64,10 +65,14 @@ class CScanner(SCons.Scanner.Recursive): dirs = () if not self.pathscanners.has_key(dirs): clone = copy.copy(self) - clone.argument = [self.fs, dirs] # XXX reaching into object + clone.hash = dirs + clone.argument = [self.fs, dirs] # XXX reaching into object self.pathscanners[dirs] = clone return self.pathscanners[dirs] + def __hash__(self): + return hash(self.hash) + def scan(filename, env, args = [SCons.Node.FS.default_fs, ()]): """ scan(str, Environment) -> [str] diff --git a/src/engine/SCons/Scanner/CTests.py b/src/engine/SCons/Scanner/CTests.py index c16da610..71b36e54 100644 --- a/src/engine/SCons/Scanner/CTests.py +++ b/src/engine/SCons/Scanner/CTests.py @@ -166,12 +166,13 @@ class CScannerTestCase6(unittest.TestCase): def runTest(self): env1 = DummyEnvironment([test.workpath("d1")]) env2 = DummyEnvironment([test.workpath("d1/d2")]) + env3 = DummyEnvironment([test.workpath("d1/../d1")]) s = SCons.Scanner.C.CScan() - s1 = s.instance(env1) - s2 = s.instance(env2) - s3 = s.instance(env1) - assert not s1 is s2 - assert s1 is s3 + s1 = s.instance(env1) + s2 = s.instance(env2) + s3 = s.instance(env3) + assert not s1 is s2 + assert s1 is s3 deps1 = s1.scan(test.workpath('f1.cpp'), None) deps2 = s2.scan(test.workpath('f1.cpp'), None) headers1 = ['f1.h', 'd1/f2.h'] @@ -179,6 +180,15 @@ class CScannerTestCase6(unittest.TestCase): deps_match(self, deps1, headers1) deps_match(self, deps2, headers2) +class CScannerTestCase7(unittest.TestCase): + def runTest(self): + s = SCons.Scanner.C.CScan() + s1 = s.instance(DummyEnvironment([test.workpath("d1")])) + s2 = s.instance(DummyEnvironment([test.workpath("d1/../d1")])) + dict = {} + dict[s1] = 777 + assert dict[s2] == 777 + def suite(): suite = unittest.TestSuite() suite.addTest(CScannerTestCase1()) @@ -187,6 +197,7 @@ def suite(): suite.addTest(CScannerTestCase4()) suite.addTest(CScannerTestCase5()) suite.addTest(CScannerTestCase6()) + suite.addTest(CScannerTestCase7()) return suite if __name__ == "__main__": diff --git a/src/engine/SCons/Scanner/ScannerTests.py b/src/engine/SCons/Scanner/ScannerTests.py index f1a92cde..e414e346 100644 --- a/src/engine/SCons/Scanner/ScannerTests.py +++ b/src/engine/SCons/Scanner/ScannerTests.py @@ -111,12 +111,22 @@ class ScannerKeywordArgumentTestCase(ScannerTestBase, unittest.TestCase): i = s.instance(env) self.test(i, env, 'i4.cpp', ['i4.h', 'i4.hpp'], arg) +class ScannerHashTestCase(ScannerTestBase, unittest.TestCase): + "Test the Scanner.Base class __hash__() method" + def runTest(self): + s = SCons.Scanner.Base(self.func, "Hash") + dict = {} + dict[s] = 777 + self.failUnless(hash(dict.keys()[0]) == hash(None), + "did not hash Scanner base class as expected") + def suite(): suite = unittest.TestSuite() suite.addTest(ScannerPositionalTestCase()) suite.addTest(ScannerKeywordTestCase()) suite.addTest(ScannerPositionalArgumentTestCase()) suite.addTest(ScannerKeywordArgumentTestCase()) + suite.addTest(ScannerHashTestCase()) return suite if __name__ == "__main__": diff --git a/src/engine/SCons/Scanner/__init__.py b/src/engine/SCons/Scanner/__init__.py index 7ecd846f..ba37edcf 100644 --- a/src/engine/SCons/Scanner/__init__.py +++ b/src/engine/SCons/Scanner/__init__.py @@ -116,6 +116,9 @@ class Base: def __cmp__(self, other): return cmp(self.__dict__, other.__dict__) + def __hash__(self): + return hash(None) + class Recursive(Base): """ The class for recursive dependency scanning. This will @@ -143,8 +146,9 @@ class Recursive(Base): else: d = self.function(f, env) d = filter(lambda x, seen=seen: str(x) not in seen, d) - deps.extend(d) - s = map(str, d) - seen.extend(s) - files.extend(s) + if d: + deps.extend(d) + s = map(str, d) + seen.extend(s) + files.extend(s) return deps -- 2.26.2