From patchwork Sun Apr 23 17:07:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Keith Seitz X-Patchwork-Id: 68191 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 38FBC3858C66 for ; Sun, 23 Apr 2023 17:08:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 38FBC3858C66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682269703; bh=HhxduC2xBZSuscDbY1EHu2k7CcSM8+952JNlaYSbbd4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Ecz01qddfxHr0daeLA+fpa6dfY7dVerWoYU03hPuyBW+9ukc7lHwXbQe6HFJWsYqM 1ot0nBvJOJOUUvQtBXmbHrrMgf7Aphqqn3c/BODHOCP8fhFglhW5J82IrDKdvhyHLR ZzFMNK4DF9ofxOrujGy6ZyxrSipuZTj9b7aOW0hA= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 35E113858C50 for ; Sun, 23 Apr 2023 17:07:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 35E113858C50 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-154-a14JD-SWPF6mqL7Dm8sqOg-1; Sun, 23 Apr 2023 13:07:56 -0400 X-MC-Unique: a14JD-SWPF6mqL7Dm8sqOg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 51A2F29A9CB0 for ; Sun, 23 Apr 2023 17:07:56 +0000 (UTC) Received: from guittard.uglyboxes.com (unknown [10.2.17.111]) by smtp.corp.redhat.com (Postfix) with ESMTP id 13FB4404E4AD for ; Sun, 23 Apr 2023 17:07:56 +0000 (UTC) To: gdb-patches@sourceware.org Subject: [PATCH] Environment variable convenience function Date: Sun, 23 Apr 2023 10:07:55 -0700 Message-Id: <20230423170755.1103121-1-keiths@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, WEIRD_QUOTING autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Keith Seitz via Gdb-patches From: Keith Seitz Reply-To: Keith Seitz Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This patch adds a new Python convenience function which simply returns the value of the given environment variable (or a default value) or throws a KeyError. (gdb) p $_env("HOME") $1 = "/home/keiths" (gdb) p $_env("IS_THIS_USEFUL") Python Exception : 'IS_THIS_USEFUL' Error occurred in Python: 'IS_THIS_USEFUL' (gdb) p $_env("IS_THIS_USEFUL", "yes") $2 = "yes" Reviewed-By: Eli Zaretskii --- gdb/NEWS | 3 +++ gdb/data-directory/Makefile.in | 1 + gdb/doc/gdb.texinfo | 7 ++++++ gdb/python/lib/gdb/function/env.py | 38 +++++++++++++++++++++++++++++ gdb/testsuite/gdb.base/default.exp | 1 + gdb/testsuite/gdb.python/py-env.c | 22 +++++++++++++++++ gdb/testsuite/gdb.python/py-env.exp | 38 +++++++++++++++++++++++++++++ 7 files changed, 110 insertions(+) create mode 100644 gdb/python/lib/gdb/function/env.py create mode 100644 gdb/testsuite/gdb.python/py-env.c create mode 100644 gdb/testsuite/gdb.python/py-env.exp diff --git a/gdb/NEWS b/gdb/NEWS index 54b5da21245..4897b1e618b 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -156,6 +156,9 @@ info main (program-counter) values, and can be used as the frame-id when calling gdb.PendingFrame.create_unwind_info. + ** New Python-based convenience function $_env(name[, default]), which + returns the string value of the environment variable of the given name. + *** Changes in GDB 13 * MI version 1 is deprecated, and will be removed in GDB 14. diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in index 39979037245..637d2fa0d55 100644 --- a/gdb/data-directory/Makefile.in +++ b/gdb/data-directory/Makefile.in @@ -109,6 +109,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/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 940315b2713..1887d0c1ae4 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -13160,6 +13160,13 @@ The type of the imaginary or real part depends on the type of the complex number, e.g., using @code{$_cimag} on a @code{float complex} will return an imaginary part of type @code{float}. +@findex $_env@r{, convenience function} +@item $_env(@var{name}@r{[}, @var{default}@r{]}) +Return the value of the environment variable named @var{name}. + +If the variable is not defined in the environment, the optional default value +is returned, if specified, or a Python exception is thrown. + @end table @value{GDBN} provides the ability to list and get help on diff --git a/gdb/python/lib/gdb/function/env.py b/gdb/python/lib/gdb/function/env.py new file mode 100644 index 00000000000..a6414ee63b4 --- /dev/null +++ b/gdb/python/lib/gdb/function/env.py @@ -0,0 +1,38 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +"""$_env""" + +import gdb +import os + +class _Env(gdb.Function): + """$_env - return the value of an environment variable. + + Usage: $_env("NAME"[, default]) + + Returns: + Value of the environment variable named NAME or DEFAULT (if supplied); + throws KeyError otherwise.""" + + def __init__(self): + super(_Env, self).__init__("_env") + + def invoke(self, name, default=None): + if default is not None and name.string() not in os.environ: + return default + return os.environ[name.string()] + +_Env() diff --git a/gdb/testsuite/gdb.base/default.exp b/gdb/testsuite/gdb.base/default.exp index 7e73db0576a..551ae7aca9b 100644 --- a/gdb/testsuite/gdb.base/default.exp +++ b/gdb/testsuite/gdb.base/default.exp @@ -628,6 +628,7 @@ if [allow_python_tests] { {$_caller_matches = } \ {$_any_caller_is = } \ {$_any_caller_matches = } \ + {$_env = } \ } } gdb_test_list_exact "show convenience" "show convenience" \ diff --git a/gdb/testsuite/gdb.python/py-env.c b/gdb/testsuite/gdb.python/py-env.c new file mode 100644 index 00000000000..d17c53b2007 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-env.c @@ -0,0 +1,22 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +void +main (void) +{ + return; +} diff --git a/gdb/testsuite/gdb.python/py-env.exp b/gdb/testsuite/gdb.python/py-env.exp new file mode 100644 index 00000000000..09a274dcfe1 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-env.exp @@ -0,0 +1,38 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This file is part of the GDB testsuite. It tests the convenience +# functions in env.py. + +load_lib gdb-python.exp + +require allow_python_tests + +standard_testfile + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} { + return -1 +} + +if {![runto_main]} { + return 0 +} + +gdb_test {p $_env("HOME")} "= \"$::env(HOME)\"" + +gdb_test {p $_env("I_DO_NOT_EXIST")} ".*KeyError.*" + +gdb_test {p $_env("I_DO_NOT_EXIST", "default value for I_DO_NOT_EXIST")} \ + {= "default value for I_DO_NOT_EXIST"}