Add a __hash_() method to the Scanners.
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Tue, 11 Dec 2001 06:18:49 +0000 (06:18 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Tue, 11 Dec 2001 06:18:49 +0000 (06:18 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@141 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/engine/SCons/Scanner/C.py
src/engine/SCons/Scanner/CTests.py
src/engine/SCons/Scanner/ScannerTests.py
src/engine/SCons/Scanner/__init__.py

index 5dbdf6f63acecb8ef49f34db308ddf41299ca6c4..589aef69937108a040669a213bd9afbb4bf6b029 100644 (file)
@@ -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]
index c16da610bd7994c1f709b84c663f598041f4b762..71b36e5427f160fadeaa90156e9bc54630323520 100644 (file)
@@ -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__":
index f1a92cdecad7a57648d276b9f3acfaa0502bc36a..e414e3461eac0383ff6d87b8339534738ba7a901 100644 (file)
@@ -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__":
index 7ecd846f31eec8d939af46ba9bf9471ca43255be..ba37edcf00ffa9ac200643839408b6b012b8bc8a 100644 (file)
@@ -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