[PATCH v2 3/3] Add a way to temporarily set a gdb parameter from Python
Lancelot SIX
lsix@lancelotsix.com
Thu Jan 13 08:50:59 GMT 2022
> +
> +
> +@contextmanager
> +def with_parameter(name, value):
> + """Temporarily set the GDB parameter NAME to VALUE.
> + Note that this is a context manager."""
> + old_value = parameter(name)
> + set_parameter(name, value)
> + # Nothing that useful to return.
> + yield None
> + set_parameter(name, old_value)
> +
> +
Hi,
In python, context managers are usually used the way RAII resources are
in c++. which is to ensure some resource is reliably released (parameter
is restored in this case) at the end of the block. Here, if an
exception is thrown within the block, the original value is not
restored.
I would suggest something like:
@contextmanager
def with_parameter(name, value):
"""Temporarily set the GDB parameter NAME to VALUE.
Note that this is a context manager."""
old_value = parameter(name)
set_parameter(name, value)
try:
yield
finally:
set_parameter(name, old_value)
This way, the parameter is restored even in case of exception, which is
what I would expect from a python user point of view.
Best,
Lancelot.
More information about the Gdb-patches
mailing list