public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: Keith Seitz <keiths@redhat.com>, gdb-patches@sourceware.org
Cc: Wendy.Peikes@netapp.com
Subject: Re: [RFC PATCH 1/2] Add $_env convenience function
Date: Fri, 03 Feb 2023 17:18:41 +0000	[thread overview]
Message-ID: <87h6w2r7ke.fsf@redhat.com> (raw)
In-Reply-To: <20230125193825.3665649-2-keiths@redhat.com>

Keith Seitz via Gdb-patches <gdb-patches@sourceware.org> writes:

> This patch adds a new Python convenience function which simply
> returns the value of the given environment variable or throws
> a KeyError.
>
> (gdb) p $_env("HOME")
> $1 = "/home/keiths"
>
> [This patch is here for the purposes of demonstarting an alternative
> design/implementation to a previously proposed patch. See
> https://sourceware.org/pipermail/gdb-patches/2022-October/192396.html .]

It probably has value on its own I think.

> ---
>  gdb/data-directory/Makefile.in     |  1 +
>  gdb/python/lib/gdb/function/env.py | 21 +++++++++++++++++++++
>  2 files changed, 22 insertions(+)
>  create mode 100644 gdb/python/lib/gdb/function/env.py
>
> diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in
> index f1139291eed..3b0a05d61dc 100644
> --- a/gdb/data-directory/Makefile.in
> +++ b/gdb/data-directory/Makefile.in
> @@ -107,6 +107,7 @@ PYTHON_FILE_LIST = \
>  	gdb/function/as_string.py \
>  	gdb/function/caller_is.py \
>  	gdb/function/strfns.py \
> +	gdb/function/env.py \
>  	gdb/printer/__init__.py \
>  	gdb/printer/bound_registers.py
>  
> diff --git a/gdb/python/lib/gdb/function/env.py b/gdb/python/lib/gdb/function/env.py
> new file mode 100644
> index 00000000000..54a441cea54
> --- /dev/null
> +++ b/gdb/python/lib/gdb/function/env.py
> @@ -0,0 +1,21 @@

Missing copyright header here.

> +"""$_env"""
> +
> +import gdb
> +import os
> +
> +class _Env(gdb.Function):
> +    """$_env - return the value of an environment variable.
> +
> +    Usage: $_env("NAME")
> +
> +    Returns:
> +      Value of the environment variable named NAME or throws KeyError if NAME is
> +      undefined in the environment."""
> +
> +    def __init__(self):
> +        super(_Env, self).__init__("_env")
> +
> +    def invoke(self, name):
> +        return os.environ[name.string()]

Something like this:

    def invoke(self, name, default=None):
        if default is not None and not name.string() in os.environ:
          return default
        return os.environ[name.string()]

would allow users to supply a default:

  p $_env("UNKNOWN", "default_value")

But retains:

  p $_env("HOME")

Might be useful?

Thanks,
Andrew

> +
> +_Env()
> -- 
> 2.38.1


  reply	other threads:[~2023-02-03 17:18 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <BYAPR06MB6117A6A68D09026F27C310D4E54C9@BYAPR06MB6117.namprd06.prod.outlook.com>
     [not found] ` <BYAPR06MB611721469E1454F0CD94259DE55D9@BYAPR06MB6117.namprd06.prod.outlook.com>
2022-10-05 23:19   ` [PATCH] gdb: allow env var specifications in cmds 'set log', 'source', 'shell' Peikes, Wendy
2022-10-05 23:19     ` Peikes, Wendy
2022-10-06 19:34     ` Keith Seitz
2022-10-26  0:25       ` Peikes, Wendy
2023-01-25 19:38         ` [RFC PATCH 0/2] Command expression evaulation substitution Keith Seitz
2023-01-25 19:38           ` [RFC PATCH 1/2] Add $_env convenience function Keith Seitz
2023-02-03 17:18             ` Andrew Burgess [this message]
2023-02-03 18:34               ` Keith Seitz
2023-01-25 19:38           ` [RFC PATCH 2/2] Allow and evaluate expressions in command arguments Keith Seitz
2023-02-03 17:22             ` Andrew Burgess
2023-02-03 18:49               ` Keith Seitz
2023-02-17 22:31             ` Pedro Alves
2023-01-25 20:21           ` [RFC PATCH 0/2] Command expression evaulation substitution Peikes, Wendy

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=87h6w2r7ke.fsf@redhat.com \
    --to=aburgess@redhat.com \
    --cc=Wendy.Peikes@netapp.com \
    --cc=gdb-patches@sourceware.org \
    --cc=keiths@redhat.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).