[Scons-users] Remove item from CPPDEFINES

Julien Pommier pommier at pianoteq.com
Fri Mar 24 10:56:06 EDT 2023


> On 24 Mar 2023, at 15:40, Mats Wichmann <mats at wichmann.us> wrote:
> 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)

Thank you — yes CPPDEFINES was previously a dict (or something dict-like) in my case, so the pop() worked. But I understand that it was probably just a consequence of the way I was filling it, and some luck. 

I’ll try your suggestion — but yes, some official method to remove entries would probably be handy.




More information about the Scons-users mailing list