Search code examples
scons

Why can't I modify a construction environment from within a pseudo builder (AddMethod)?


I need to modify a set of variables in multiple construction environments so I decided the cleanest way would be to create a method that does that. Instead of a vanilla python function I've tried to use the AddMethod in the construction environment object since it seems to be the way of doing such things in scons.

However, it appears that methods of construction environments are unable to modify this environment.

Here's an example where I tested a few ways of changing a variable in a construction environment:

env1 = Environment(VAR='foo')
def changeVal(env, newval):
    env['VAL'] = newval
env1.AddMethod(changeVal, 'ChangeVal')
env2 = env1.Clone(VAR='bar')
env3 = env2.Clone()
env3['VAR'] = 'baz'
env4 = env3.Clone()
env4.ChangeVal('qux')
print(env1['VAR'], env2['VAR'], env3['VAR'], env4['VAR'])

The result is foo bar baz baz while I would expect foo bar baz qux. Why is that?


Solution

  • Pretty sure you have a typo in your example:

    env1 = Environment(VAR='foo')
    def changeVal(env, newval):
        env['VAL'] = newval
    env1.AddMethod(changeVal, 'ChangeVal')
    env2 = env1.Clone(VAR='bar')
    env3 = env2.Clone()
    env3['VAR'] = 'baz'
    env4 = env3.Clone()
    env4.ChangeVal('qux')
    print(env1['VAR'], env2['VAR'], env3['VAR'], env4['VAR'])
    

    Specifically

    def changeVal(env, newval):
        env['VAR'] = newval
        #.   ^^^--- You have VAL here, but check for VAR later.