Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[develop2] Improve settings.rm_safe() #12379

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)