[RFC,1/2] Add $_env convenience function

Message ID 20230125193825.3665649-2-keiths@redhat.com
State New
Headers
Series Command expression evaulation substitution |

Commit Message

Keith Seitz Jan. 25, 2023, 7:38 p.m. UTC
  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 .]
---
 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
  

Comments

Andrew Burgess Feb. 3, 2023, 5:18 p.m. UTC | #1
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
  
Keith Seitz Feb. 3, 2023, 6:34 p.m. UTC | #2
On 2/3/23 09:18, Andrew Burgess wrote:

>> 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.

It *was* just an RFC. :-P

I will, of course, add appropriate header, copyright, etc
when I submit for final approval (after I write a few tests
and docs).

I will submit independently of the expression-evaluation
substitution patch (#2 in this series).

> 
>> +"""$_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?

That's a very good idea. I'll steal that. :-)

Keith
  

Patch

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 @@ 
+"""$_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()]
+
+_Env()