Issue 2238: Add a __contains__() method to support the "val in env"
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Sun, 9 Nov 2008 14:47:35 +0000 (14:47 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Sun, 9 Nov 2008 14:47:35 +0000 (14:47 +0000)
statement for construction environments.

git-svn-id: http://scons.tigris.org/svn/scons/trunk@3775 fdb21ef1-2011-0410-befe-b5e4ea1792b1

src/CHANGES.txt
src/engine/SCons/Environment.py
src/engine/SCons/EnvironmentTests.py

index b481617de6abe41e1275cbd96239660394feecea..5abdb2914f5bdd43f3f889a3f0f12bab13afe11d 100644 (file)
@@ -46,6 +46,9 @@ RELEASE 1.X - XXX
     - Speed up the internal find_file() function (used for searching
       CPPPATH, LIBPATH, etc.).
 
+    - Add support for using the Python "in" keyword on construction
+      environments (for example, if "CPPPATH" in env: ...).
+
   From Rob Managan:
 
     - Scan for TeX files in the paths specified in the $TEXINPUTS
index 327e0d108cb60832be22e26c90b898c5c772e13a..08733541581a9d5a81d6e0891181a5c432a44267 100644 (file)
@@ -399,6 +399,9 @@ class SubstitutionEnvironment:
     def has_key(self, key):
         return self._dict.has_key(key)
 
+    def __contains__(self, key):
+        return self._dict.__contains__(key)
+
     def items(self):
         return self._dict.items()
 
@@ -2169,6 +2172,10 @@ class OverrideEnvironment(Base):
             return 1
         except KeyError:
             return self.__dict__['__subject'].has_key(key)
+    def __contains__(self, key):
+        if self.__dict__['overrides'].__contains__(key):
+            return 1
+        return self.__dict__['__subject'].__contains__(key)
     def Dictionary(self):
         """Emulates the items() method of dictionaries."""
         d = self.__dict__['__subject'].Dictionary().copy()
index 62530baa2d00689de0ab16136666665da98f170b..f738891de003d4178c0fc8457558243e3fb6c80d 100644 (file)
@@ -233,6 +233,21 @@ class SubstitutionTestCase(unittest.TestCase):
         assert env.has_key('XXX')
         assert not env.has_key('YYY')
 
+    def test_contains(self):
+        """Test the SubstitutionEnvironment __contains__() method
+        """
+        try:
+            'x' in {'x':1}
+        except TypeError:
+            # TODO(1.5)
+            # An early version of Python that doesn't support "in"
+            # on dictionaries.  Just pass the test.
+            pass
+        else:
+            env = SubstitutionEnvironment(XXX = 'x')
+            assert 'XXX' in env
+            assert not 'YYY' in env
+
     def test_items(self):
         """Test the SubstitutionEnvironment items() method
         """
@@ -3507,6 +3522,27 @@ class OverrideEnvironmentTestCase(unittest.TestCase,TestEnvironmentFixture):
         assert not env2.has_key('ZZZ'), env2.has_key('ZZZ')
         assert env3.has_key('ZZZ'), env3.has_key('ZZZ')
 
+    def test_contains(self):
+        """Test the OverrideEnvironment __contains__() method"""
+        try:
+            'x' in {'x':1}
+        except TypeError:
+            # TODO(1.5)
+            # An early version of Python that doesn't support "in"
+            # on dictionaries.  Just pass the test.
+            pass
+        else:
+            env, env2, env3 = self.envs
+            assert 'XXX' in env
+            assert 'XXX' in env2
+            assert 'XXX' in env3
+            assert 'YYY' in env
+            assert 'YYY' in env2
+            assert 'YYY' in env3
+            assert not 'ZZZ' in env
+            assert not 'ZZZ' in env2
+            assert 'ZZZ' in env3
+
     def test_items(self):
         """Test the OverrideEnvironment Dictionary() method"""
         env, env2, env3 = self.envs