Skip to content

Commit

Permalink
env.unset method
Browse files Browse the repository at this point in the history
  • Loading branch information
bruchar1 committed Oct 10, 2023
1 parent 3af0632 commit 884325a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/markdown/snippets/environment_unset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## New `unset()` method on `environment` objects

[[env]] now have an [[env.unset]] method to ensure an existing environment
is *not* defined.
6 changes: 6 additions & 0 deletions docs/yaml/objects/env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,9 @@ methods:
description: The values to set

kwargs_inherit: env.append

- name: unset
returns: void
description: |
Unset the specified environment variable. If this variable does not exist,
nothing happens.
7 changes: 7 additions & 0 deletions mesonbuild/interpreter/interpreterobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ class EnvironmentVariablesHolder(ObjectHolder[mesonlib.EnvironmentVariables], Mu
def __init__(self, obj: mesonlib.EnvironmentVariables, interpreter: 'Interpreter'):
super().__init__(obj, interpreter)
self.methods.update({'set': self.set_method,
'unset': self.unset_method,
'append': self.append_method,
'prepend': self.prepend_method,
})
Expand All @@ -309,6 +310,12 @@ def set_method(self, args: T.Tuple[str, T.List[str]], kwargs: 'EnvironmentSepara
name, values = args
self.held_object.set(name, values, kwargs['separator'])

@FeatureNew('environment.unset()', '1.3.0')
@typed_pos_args('environment.unset', str)
@noKwargs
def unset_method(self, args: T.Tuple[str], kwargs: TYPE_kwargs) -> None:
self.held_object.unset(args[0])

@typed_pos_args('environment.append', str, varargs=str, min_varargs=1)
@typed_kwargs('environment.append', ENV_SEPARATOR_KW)
def append_method(self, args: T.Tuple[str, T.List[str]], kwargs: 'EnvironmentSeparatorKW') -> None:
Expand Down
18 changes: 18 additions & 0 deletions mesonbuild/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def __init__(self, values: T.Optional[EnvInitValueType] = None,
self.envvars: T.List[T.Tuple[T.Callable[[T.Dict[str, str], str, T.List[str], str, T.Optional[str]], str], str, T.List[str], str]] = []
# The set of all env vars we have operations for. Only used for self.has_name()
self.varnames: T.Set[str] = set()
self.unset_vars: T.Set[str] = set()

if values:
init_func = getattr(self, init_method)
Expand Down Expand Up @@ -103,18 +104,33 @@ def merge(self, other: EnvironmentVariables) -> None:
for method, name, values, separator in other.envvars:
self.varnames.add(name)
self.envvars.append((method, name, values, separator))
if name in self.unset_vars:
self.unset_vars.remove(name)
self.unset_vars.update(other.unset_vars)

def set(self, name: str, values: T.List[str], separator: str = os.pathsep) -> None:
self.varnames.add(name)
self.envvars.append((self._set, name, values, separator))
if name in self.unset_vars:
self.unset_vars.remove(name)

def unset(self, name: str) -> None:
if name in self.varnames:
self.varnames.remove(name)
self.envvars = [(m, n, v, s) for (m, n, v, s) in self.envvars if n != name]
self.unset_vars.add(name)

def append(self, name: str, values: T.List[str], separator: str = os.pathsep) -> None:
self.varnames.add(name)
self.envvars.append((self._append, name, values, separator))
if name in self.unset_vars:
self.unset_vars.remove(name)

def prepend(self, name: str, values: T.List[str], separator: str = os.pathsep) -> None:
self.varnames.add(name)
self.envvars.append((self._prepend, name, values, separator))
if name in self.unset_vars:
self.unset_vars.remove(name)

@staticmethod
def _set(env: T.Dict[str, str], name: str, values: T.List[str], separator: str, default_value: T.Optional[str]) -> str:
Expand All @@ -135,6 +151,8 @@ def get_env(self, full_env: EnvironOrDict, default_fmt: T.Optional[str] = None)
for method, name, values, separator in self.envvars:
default_value = default_fmt.format(name) if default_fmt else None
env[name] = method(env, name, values, separator, default_value)
for name in self.unset_vars:
env.pop(name, None)
return env


Expand Down

0 comments on commit 884325a

Please sign in to comment.