From: stevenknight Date: Sun, 9 Nov 2008 14:47:35 +0000 (+0000) Subject: Issue 2238: Add a __contains__() method to support the "val in env" X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=1335a3f7948cbb6effaa96b0dc5264f6a597c670;p=scons.git Issue 2238: Add a __contains__() method to support the "val in env" statement for construction environments. git-svn-id: http://scons.tigris.org/svn/scons/trunk@3775 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- diff --git a/src/CHANGES.txt b/src/CHANGES.txt index b481617d..5abdb291 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -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 diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index 327e0d10..08733541 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -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() diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index 62530baa..f738891d 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -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