From: Lancelot SIX via Gdb-patches <gdb-patches@sourceware.org>
To: Tom Tromey <tromey@adacore.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH v2 3/3] Add a way to temporarily set a gdb parameter from Python
Date: Thu, 13 Jan 2022 08:50:59 +0000 [thread overview]
Message-ID: <20220113085059.z3rm44jun2sconai@Plymouth> (raw)
In-Reply-To: <20220110215144.1714909-4-tromey@adacore.com>
> +
> +
> +@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.
next prev parent reply other threads:[~2022-01-13 8:51 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-10 21:51 [PATCH v2 0/3] Improvements to Python parameters Tom Tromey via Gdb-patches
2022-01-10 21:51 ` [PATCH v2 1/3] Change how Python architecture and language are handled Tom Tromey via Gdb-patches
2022-01-31 13:20 ` Simon Marchi via Gdb-patches
2022-01-10 21:51 ` [PATCH v2 2/3] Fix another crash with gdb parameters in Python Tom Tromey via Gdb-patches
2022-01-10 21:51 ` [PATCH v2 3/3] Add a way to temporarily set a gdb parameter from Python Tom Tromey via Gdb-patches
2022-01-13 8:50 ` Lancelot SIX via Gdb-patches [this message]
2022-01-13 19:46 ` Tom Tromey via Gdb-patches
2022-01-26 14:06 ` [PATCH v2 0/3] Improvements to Python parameters Tom Tromey via Gdb-patches
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220113085059.z3rm44jun2sconai@Plymouth \
--to=gdb-patches@sourceware.org \
--cc=lsix@lancelotsix.com \
--cc=tromey@adacore.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox