Change the env.Dictionary to an access method for an env._dict attribute.
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Wed, 26 Sep 2001 13:46:18 +0000 (13:46 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Wed, 26 Sep 2001 13:46:18 +0000 (13:46 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@70 fdb21ef1-2011-0410-befe-b5e4ea1792b1

doc/design/engine.sgml
src/engine/SCons/Environment.py
src/engine/SCons/EnvironmentTests.py

index 3bb636b0754daeffed8e1c4c2d6e3d475b53ea79..92343bf3f2ce45e3b000f01f8dc71ce476828c01 100644 (file)
        dict = env.Dictionary()
        </programlisting>
 
-<REMARK>
-In the current source code, I implemented this as a dictionary attribute
-named <literal>Dictionary</literal>.  While reasonably Pythonic, this
-is ultimately Not Good.  We don't want people using a reference to the
-dictionary to change construction variables out from under an existing
-environment.  We should use an internal <literal>_dict</literal>
-attribute and control access to it through a method, as specified above.
-</REMARK>
-
   <para>
 
    If any arguments are supplied, then just the corresponding value(s)
index 5561cceda91ee15de74565309d5ded06422ffeb7..508392ba254c082fdd18ba8f7118c07557fd4dfb 100644 (file)
@@ -53,7 +53,7 @@ class Environment:
     """
 
     def __init__(self, **kw):
-       self.Dictionary = {}
+       self._dict = {}
        if kw.has_key('BUILDERS'):
            builders = kw['BUILDERS']
            if not type(builders) is types.ListType:
@@ -61,7 +61,7 @@ class Environment:
        else:
            import SCons.Defaults
            kw['BUILDERS'] = SCons.Defaults.Builders[:]
-       self.Dictionary.update(copy.deepcopy(kw))
+       self._dict.update(copy.deepcopy(kw))
 
        class BuilderWrapper:
            """Wrapper class that allows an environment to
@@ -83,7 +83,7 @@ class Environment:
 
 
     def __cmp__(self, other):
-       return cmp(self.Dictionary, other.Dictionary)
+       return cmp(self._dict, other._dict)
 
     def Builders(self):
        pass    # XXX
@@ -107,7 +107,7 @@ class Environment:
        """Update an existing construction Environment with new
        construction variables and/or values.
        """
-       self.Dictionary.update(copy.deepcopy(kw))
+       self._dict.update(copy.deepcopy(kw))
 
     def        Depends(self, target, dependency):
        """Explicity specify that 'target's depend on 'dependency'."""
@@ -120,6 +120,14 @@ class Environment:
            tlist = tlist[0]
        return tlist
 
+    def Dictionary(self, *args):
+       if not args:
+           return self._dict
+       dlist = map(lambda x, s=self: s._dict[x], args)
+       if len(dlist) == 1:
+           dlist = dlist[0]
+       return dlist
+
     def subst(self, string):
        """Recursively interpolates construction variables from the
        Environment into the specified string, returning the expanded
@@ -134,7 +142,7 @@ class Environment:
            key = m.group(1)
            if key[:1] == '{' and key[-1:] == '}':
                key = key[1:-1]
-           if _self.Dictionary.has_key(key): return _self.Dictionary[key]
+           if _self._dict.has_key(key): return _self._dict[key]
            else: return ''
        n = 1
        while n != 0:
index 149ba74813dab0a7b642f87f490700e22a0f15db..0488173114a7e26a442696944710299cc27e9195 100644 (file)
@@ -75,9 +75,9 @@ class EnvironmentTestCase(unittest.TestCase):
        assert env1 == env1copy
 
        env3 = env1.Copy(XXX = 'x3', ZZZ = 'z3')
-       assert env3.Dictionary['XXX'] == 'x3'
-       assert env3.Dictionary['YYY'] == 'y'
-       assert env3.Dictionary['ZZZ'] == 'z3'
+       assert env3.Dictionary('XXX') == 'x3'
+       assert env3.Dictionary('YYY') == 'y'
+       assert env3.Dictionary('ZZZ') == 'z3'
        assert env1 == env1copy
 
     def test_Dictionary(self):
@@ -86,10 +86,14 @@ class EnvironmentTestCase(unittest.TestCase):
        Fetch them from the Dictionary and check for well-known
        defaults that get inserted.
        """
-       env = Environment(XXX = 'x', YYY = 'y')
-       assert env.Dictionary['XXX'] == 'x'
-       assert env.Dictionary['YYY'] == 'y'
-       assert env.Dictionary.has_key('BUILDERS')
+       env = Environment(XXX = 'x', YYY = 'y', ZZZ = 'z')
+       assert env.Dictionary('XXX') == 'x'
+       assert env.Dictionary('YYY') == 'y'
+       assert env.Dictionary('XXX', 'ZZZ') == ['x', 'z']
+       xxx, zzz = env.Dictionary('XXX', 'ZZZ')
+       assert xxx == 'x'
+       assert zzz == 'z'
+       assert env.Dictionary().has_key('BUILDERS')
 
     def test_Environment(self):
        """Test construction Environments creation