Skip to content

Commit

Permalink
[develop2] Improve settings.rm_safe() (#12379)
Browse files Browse the repository at this point in the history
* compatibility.py problem with application C and cppstd

* fixed

* fix tests
  • Loading branch information
memsharded authored Nov 11, 2022
1 parent 6578019 commit 24f761c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
32 changes: 21 additions & 11 deletions conans/model/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ def get_definition(self):
ret[key] = value.get_definition()
return ret

def rm_safe(self, name):
""" Iterates all possible subsettings, calling rm_safe() for all of them. If removing
"compiler.cppstd", this will iterate msvc, gcc, clang, etc, calling rm_safe(cppstd) for
all of them"""
if isinstance(self._definition, list):
return
for subsetting in self._definition.values():
subsetting.rm_safe(name)


class Settings(object):
def __init__(self, definition=None, name="settings", parent_value="settings"):
Expand Down Expand Up @@ -199,17 +208,18 @@ def get_safe(self, name, default=None):
return default

def rm_safe(self, name):
try:
tmp = self
attr_ = name
if "." in name:
fields = name.split(".")
attr_ = fields.pop()
for prop in fields:
tmp = getattr(tmp, prop)
delattr(tmp, attr_)
except ConanException:
pass
""" Removes the setting or subsetting from the definition. For example,
rm_safe("compiler.cppstd") remove all "cppstd" subsetting from all compilers, irrespective
of the current value of the "compiler"
"""
if "." in name:
setting, remainder = name.split(".", 1) # setting=compiler, remainder = cppstd
try:
self._data[setting].rm_safe(remainder) # call rm_safe("cppstd") for the "compiler"
except KeyError:
pass
else:
self._data.pop(name, None)

def copy(self):
""" deepcopy, recursive
Expand Down
20 changes: 20 additions & 0 deletions conans/test/unittests/model/settings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,23 @@ def test_get_definition(self):
sot = settings.get_definition()
assert [None, "cygwin", "msys", "msys2", "wsl"] == sot["os"]["Windows"]["subsystem"]


def test_rm_safe():
settings = Settings.loads(default_settings_yml)
settings.rm_safe("compiler.cppstd")
settings.rm_safe("compiler.libcxx")
settings.compiler = "gcc"
with pytest.raises(Exception) as e:
settings.compiler.cppstd = "14"
assert "'settings.compiler.cppstd' doesn't exist for 'gcc'" in str(e.value)
with pytest.raises(Exception) as e:
settings.compiler.libcxx = "libstdc++"
assert "'settings.compiler.libcxx' doesn't exist for 'gcc'" in str(e.value)

settings.compiler = "clang"
with pytest.raises(Exception) as e:
settings.compiler.cppstd = "14"
assert "'settings.compiler.cppstd' doesn't exist for 'clang'" in str(e.value)
with pytest.raises(Exception) as e:
settings.compiler.libcxx = "libstdc++"
assert "'settings.compiler.libcxx' doesn't exist for 'clang'" in str(e.value)

0 comments on commit 24f761c

Please sign in to comment.