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.133.124]) by sourceware.org (Postfix) with ESMTPS id A2FD63858D37 for ; Sun, 17 Mar 2024 14:08:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A2FD63858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A2FD63858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710684483; cv=none; b=tmC7/iGtfEBUr3QfWMTZPl1vY5gDoh4iWx6apnx/upc8SgBWcI3+Qem5nWeuySOjB9FZhSMmXDKTDG7qRtHgrmq0NiN2NUzKT7N1JxY+C84ON9W32cyddLmKiuQEHIpeNQnfhd88QgpxPBml+OGldKRnxXnksf466s993WU88j0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710684483; c=relaxed/simple; bh=RgPei+JcROwostSte0Q5uLblelOWqXTIaKTV/SaKnEQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZwMSYZo+fp1Kb722a2JJ+O9M8R30XQgrpJtm8ftQICQFT550afpZ034UHSsiNycxZTg+t5fmx30HNT3T/VRCzaddi0lO8iuFwAlY2HtTztdlloFNlDP8iJ3nvH8tVXIueAd0brriyWgliQfACoYTLhLk2xQagrsKGGIddQxoFLY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710684481; 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: in-reply-to:in-reply-to:references:references; bh=PrQjAEjzBKWzy9phaXyKff2llA1xoZllKdqr8BwR5SI=; b=Yyh0hPrDXNmvCwY0aLhwT1V7b4NsBH6nul4qS+IqYiJfbvEFdpO5Fk18v8M/c8SjN/Pvjh 7C7RmG9bmrFHjmrkgvStEG3wnUseuNFrTdkSlCkulauqhfHBiEYDIUczGhLjTagmluxVrM K/wJtvf38broYzOQSBBQxGYD2OjQV7Q= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-WTZCj6eSMCa0Bcr_sMO-Jg-1; Sun, 17 Mar 2024 10:07:59 -0400 X-MC-Unique: WTZCj6eSMCa0Bcr_sMO-Jg-1 Received: by mail-ed1-f69.google.com with SMTP id 4fb4d7f45d1cf-5684345c0ebso5297915a12.1 for ; Sun, 17 Mar 2024 07:07:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710684477; x=1711289277; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PrQjAEjzBKWzy9phaXyKff2llA1xoZllKdqr8BwR5SI=; b=bTeT3B1JSoAu/lidWwYpIzA1ABR4N4Ky/HJkCTAWYm0C5+RPrIQ4nx1cSVIwa8Ldyh a9x+w7wmlBL6zb3yhVTnwOii9WKWx6uwhRJQwA0uz5R24FUJ1h6veY3ENM/DLt3Tl3v1 NHEoPxBKON3cb+I9PT3zYeNF5udiEofxU03kmJqrEXmo0bJzjpN8fPyyPtJnjz6oYqA8 EhtO3SAgkIq9ehCmXx+rLmF2YDeWJ8XCIg6SdkWf4iem7RRgqQNhMiqUcC0RL/VDG+o4 q+vxOS6ClznrH7RDk9Wb//FKNcgIk8bwoUUT37cUVYHBNsYCGCmtXO0+h+r5zeS2pjj0 gWpA== X-Forwarded-Encrypted: i=1; AJvYcCVJ9DbszQP+UfjXOHnfsVJjx9GmO9UqtND3TZhnm54P1BGEOYx+8iVIS320Eu6e6JPt8cRuNtHEd+zDH5kew8iDJ8V9xB4L8JBh8A== X-Gm-Message-State: AOJu0YyH9zyUo5fyJ0h6pXwAqQZ9n6Qmf5kE9YndQGTadyAmWtSwWbH5 C7UH47/UUL1O0BbVSOZQchyIcclB0ivEU2/jWCPAtEcVeAgYfcZX6LSmduMUid+OklzCRnc6vZU CWanayCCQTIeVyjrVTxqo8GM+aMRNSuTJqeJ4ZpJ1UI83Y8GAQj65Epai+aJaryb53KA= X-Received: by 2002:a17:906:6c8e:b0:a46:b9eb:ae5 with SMTP id s14-20020a1709066c8e00b00a46b9eb0ae5mr894345ejr.24.1710684477079; Sun, 17 Mar 2024 07:07:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEFgSQEpEsSPzDnfgvZJe26A4DLz2oqdYFxYo5N4yKlZbQwvsd4K19c+FVzaM8KwnvWPUDPoQ== X-Received: by 2002:a17:906:6c8e:b0:a46:b9eb:ae5 with SMTP id s14-20020a1709066c8e00b00a46b9eb0ae5mr894331ejr.24.1710684476502; Sun, 17 Mar 2024 07:07:56 -0700 (PDT) Received: from localhost ([109.144.22.125]) by smtp.gmail.com with ESMTPSA id h25-20020a17090634d900b00a46196a7faesm3785112ejb.57.2024.03.17.07.07.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Mar 2024 07:07:56 -0700 (PDT) From: Andrew Burgess To: Tom Tromey , gdb-patches@sourceware.org Cc: Tom Tromey Subject: Re: [PATCH] Set __file__ when source'ing a Python script In-Reply-To: <20240308192543.3578375-1-tromey@adacore.com> References: <20240308192543.3578375-1-tromey@adacore.com> Date: Sun, 17 Mar 2024 14:07:49 +0000 Message-ID: <87h6h5uf2i.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-12.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_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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: Tom Tromey writes: > This patch arranges to set __file__ when source'ing a Python script. > This fixes a problem that was introduced by the "source" rewrite, and > then pointed out by Lancelot Six. LGTM. Approved-By: Andrew Burgess Thanks, Andrew > --- > gdb/python/python.c | 73 +++++++++++++++++++++++++---- > gdb/testsuite/gdb.python/source2.py | 3 ++ > 2 files changed, 66 insertions(+), 10 deletions(-) > > diff --git a/gdb/python/python.c b/gdb/python/python.c > index 57f6bd571d5..e2ac315f9f5 100644 > --- a/gdb/python/python.c > +++ b/gdb/python/python.c > @@ -288,12 +288,13 @@ gdbpy_check_quit_flag (const struct extension_language_defn *extlang) > > /* Evaluate a Python command like PyRun_SimpleString, but takes a > Python start symbol, and does not automatically print the stack on > - errors. FILENAME is used to set the file name in error > - messages. */ > + errors. FILENAME is used to set the file name in error messages; > + NULL means that this is evaluating a string, not the contents of a > + file. */ > > static int > eval_python_command (const char *command, int start_symbol, > - const char *filename = "") > + const char *filename = nullptr) > { > PyObject *m, *d; > > @@ -305,17 +306,69 @@ eval_python_command (const char *command, int start_symbol, > if (d == NULL) > return -1; > > + bool file_set = false; > + if (filename != nullptr) > + { > + gdbpy_ref<> file = host_string_to_python_string ("__file__"); > + if (file == nullptr) > + return -1; > + > + /* PyDict_GetItemWithError returns a borrowed reference. */ > + PyObject *found = PyDict_GetItemWithError (d, file.get ()); > + if (found == nullptr) > + { > + if (PyErr_Occurred ()) > + return -1; > + > + gdbpy_ref<> filename_obj = host_string_to_python_string (filename); > + if (filename_obj == nullptr) > + return -1; > + > + if (PyDict_SetItem (d, file.get (), filename_obj.get ()) < 0) > + return -1; > + if (PyDict_SetItemString (d, "__cached__", Py_None) < 0) > + return -1; > + > + file_set = true; > + } > + } > + > /* Use this API because it is in Python 3.2. */ > - gdbpy_ref<> code (Py_CompileStringExFlags (command, filename, start_symbol, > + gdbpy_ref<> code (Py_CompileStringExFlags (command, > + filename == nullptr > + ? "" > + : filename, > + start_symbol, > nullptr, -1)); > - if (code == nullptr) > - return -1; > > - gdbpy_ref<> result (PyEval_EvalCode (code.get (), d, d)); > - if (result == nullptr) > - return -1; > + int result = -1; > + if (code != nullptr) > + { > + gdbpy_ref<> eval_result (PyEval_EvalCode (code.get (), d, d)); > + if (eval_result != nullptr) > + result = 0; > + } > + > + if (file_set) > + { > + /* If there's already an exception occurring, preserve it and > + restore it before returning from this function. */ > + std::optional save_error; > + if (result < 0) > + save_error.emplace (); > + > + /* CPython also just ignores errors here. These should be > + expected to be exceedingly rare anyway. */ > + if (PyDict_DelItemString (d, "__file__") < 0) > + PyErr_Clear (); > + if (PyDict_DelItemString (d, "__cached__") < 0) > + PyErr_Clear (); > > - return 0; > + if (save_error.has_value ()) > + save_error->restore (); > + } > + > + return result; > } > > /* Implementation of the gdb "python-interactive" command. */ > diff --git a/gdb/testsuite/gdb.python/source2.py b/gdb/testsuite/gdb.python/source2.py > index 60d59d9056e..79dc1c26524 100644 > --- a/gdb/testsuite/gdb.python/source2.py > +++ b/gdb/testsuite/gdb.python/source2.py > @@ -15,4 +15,7 @@ > # You should have received a copy of the GNU General Public License > # along with this program. If not, see . > > +# Make sure __file__ is defined. > +assert type(__file__) == str > + > print("y%ss" % "e") > -- > 2.43.0