[Scons-users] Remove item from CPPDEFINES

Mats Wichmann mats at wichmann.us
Fri Mar 24 10:40:00 EDT 2023


On 3/24/23 07:12, Julien Pommier wrote:
> Hi,
> 
> I used to remove an entry from my CPPDEFINES with the pop() method:
> 
> env["CPPDEFINES"].pop(“MYDEFINE”, None)
> 
> But this is not working anymore with scons 4.5.2.
> 
> Is there a recommended way to do that ?

It depends.  You are expecting CPPDEFINES to be a dict? Because 
otherwise that syntax wouldn't work...

The basic concept is that for consistency you shouldn't mix direct 
Python access to CPPDEFINES and SCons API access to CPPDEFINES, because 
the type containing CPPDEFINES is not guaranteed if the SCons APIs have 
worked on it (this was also true before the refactor, though the 
internal details differ). I'm a little surprised to see there isn't a 
method for removals, maybe there should be for consistency.

Before the change, CPPDEFINES was likely a list, but it might also be a 
dict (is this what you're expecting?), or a string, or a tuple, 
depending on previous modifications, each of which need different 
handling to remove an item. After the change it could be any of those, 
or a deque.

If you're sure CPPDEFINES is a deque (the new internal representation 
after at least two writes to CPPDEFINES like Append, ParseConfig, 
ParseFlags, etc.), you can use "remove" with a try block.  The same will 
work if it's a list - both raise ValueError if there isn't a match.  Be 
aware that there have always been subtleties about this - CPPDEFINES 
members can be a string, possibly including a value: "FOO" or "FOO=1"), 
or a tuple ("FOO", ), ("FOO", "1"), and in trying to "remove" it or find 
its "index" you need to match that exactly. For a dictionary type it's 
easier, because of the way a dictionary's pop method is written, you 
only need to match the key.

You can also make a new entity from the old one and write that back to 
env['CPPDEFINES'], it won't break things if you convert the internal 
storage to a different type, perhaps something like this:

try:
     env['CPPDEFINES'].pop("MYDEFINE", None)
except TypeError:
    env['CPPDEFINES'] = dict(env['CPPDEFINES'])
    env['CPPDEFINES'].pop("MYDEFINE", None)





More information about the Scons-users mailing list