From cdd68fba46bb94e627c78ae0f220c59db7f2a889 Mon Sep 17 00:00:00 2001 From: stevenknight Date: Wed, 26 Sep 2001 13:46:18 +0000 Subject: [PATCH] Change the env.Dictionary to an access method for an env._dict attribute. git-svn-id: http://scons.tigris.org/svn/scons/trunk@70 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- doc/design/engine.sgml | 9 --------- src/engine/SCons/Environment.py | 18 +++++++++++++----- src/engine/SCons/EnvironmentTests.py | 18 +++++++++++------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/doc/design/engine.sgml b/doc/design/engine.sgml index 3bb636b0..92343bf3 100644 --- a/doc/design/engine.sgml +++ b/doc/design/engine.sgml @@ -152,15 +152,6 @@ dict = env.Dictionary() - -In the current source code, I implemented this as a dictionary attribute -named Dictionary. 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 _dict -attribute and control access to it through a method, as specified above. - - If any arguments are supplied, then just the corresponding value(s) diff --git a/src/engine/SCons/Environment.py b/src/engine/SCons/Environment.py index 5561cced..508392ba 100644 --- a/src/engine/SCons/Environment.py +++ b/src/engine/SCons/Environment.py @@ -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: diff --git a/src/engine/SCons/EnvironmentTests.py b/src/engine/SCons/EnvironmentTests.py index 149ba748..04881731 100644 --- a/src/engine/SCons/EnvironmentTests.py +++ b/src/engine/SCons/EnvironmentTests.py @@ -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 -- 2.26.2