From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 256743858D20 for ; Fri, 3 Feb 2023 18:34:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 256743858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675449297; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RA8i/YCFbkeP0828oYMZ0esYf8AbBN9wtmdo6ODv0Ao=; b=VJWOaiG8trlj0VhGjoIKwuN2EazE2HiUwAT/lV46b3ELURe9+H5mJtoJvRQxAOjmaUJvYm SNOXSsikM0WHw3O3l5Ph+LVnOWiOOzvK8lkmLFokOn/bMJ4Qt3Ln7T6XlPeXft74Nzl4qf swq9IXyhWCqhwv8PMcpp0KWGYLxfEdQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-381-EYn18EfeM5yjVo3alosRXg-1; Fri, 03 Feb 2023 13:34:56 -0500 X-MC-Unique: EYn18EfeM5yjVo3alosRXg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 337EC185A7A4; Fri, 3 Feb 2023 18:34:56 +0000 (UTC) Received: from [10.2.16.136] (unknown [10.2.16.136]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B73A12026D37; Fri, 3 Feb 2023 18:34:55 +0000 (UTC) Message-ID: <053131f9-2138-d1d3-b03d-90d2d67b3986@redhat.com> Date: Fri, 3 Feb 2023 10:34:54 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: Re: [RFC PATCH 1/2] Add $_env convenience function To: Andrew Burgess , gdb-patches@sourceware.org Cc: Wendy.Peikes@netapp.com References: <20230125193825.3665649-1-keiths@redhat.com> <20230125193825.3665649-2-keiths@redhat.com> <87h6w2r7ke.fsf@redhat.com> From: Keith Seitz In-Reply-To: <87h6w2r7ke.fsf@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,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 List-Id: 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