From patchwork Fri Mar 8 19:25:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 86993 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 04A18385E002 for ; Fri, 8 Mar 2024 19:26:17 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x133.google.com (mail-il1-x133.google.com [IPv6:2607:f8b0:4864:20::133]) by sourceware.org (Postfix) with ESMTPS id B73073858D33 for ; Fri, 8 Mar 2024 19:25:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B73073858D33 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B73073858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::133 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709925956; cv=none; b=bURMMMxggrGZ/S62a38qbT9d9ihsnlX6mX0qkUPgFPJv/ABz1R78qRZwRiwKEzlaTfLGizErlOjjaK3NsM8EmcyeizXqzSY5kd3eUVyZhS0Ttwf9U3K95VU6OU6zSBudLQwnlY1pPAkVEPWkNGu5ciZVMui/jvgKtzZ2ZIS90Mw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709925956; c=relaxed/simple; bh=E19KZQkoD8Ws4pRkaD/WLGKRa4fOnNhG75DaoQmDYEo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PCIcPcRr6hOE/JEiBSwTBNuDWHi3lMGCodvaetK5t+5l+ft4Ex3wyiDKO5V6U21aOOKTE3N/WVYBUeWN/hUdBS1Qg+PmDHOUyjYGCmOYQCf420qQNe/Wf2/ADwPM4JU/K9frJCodPEm/DWJTnG2l3Nl33/RewHS5FYFGjrYzLH8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x133.google.com with SMTP id e9e14a558f8ab-3663080b6a4so3103275ab.0 for ; Fri, 08 Mar 2024 11:25:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1709925954; x=1710530754; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Mo+fGm60FVPHnZlGAjof2K2jbA/H0vkBkxlBRyq6PvU=; b=JWVkIkASd++wjW2p+37cWI8H342nyZ9erVVKUrU7OKhbMNUtopPv4WQOpqdkIgCthR mvdHYKu4RDqhKt0LWtXwK2U7noL4GVzTMk8DKoOsFCCEIeXfZDq7C+/mHhyPm7C6FsEG KCnEtekJLRPrnQUMurGdGKu+Xie+yIydgqUuaWjEw2lc5ApScy+zCOUoLkI0kWdwwmfg L7eHjkVhYblBgNuuNvTTFyN0umUp5Zz7KKcbFQgA1i5CK4GqLk2H62EpMUBm0EXu322s nH2gjYzNKguaFl8vUp8ByKZlnzKxoQs2rfQq0fmoRTHfx5BLdLtDUgm5SsWApobpSS6S KC2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709925954; x=1710530754; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Mo+fGm60FVPHnZlGAjof2K2jbA/H0vkBkxlBRyq6PvU=; b=pab0AziGk6KJsH519UEcSPYHV7pSWgtojlBFIdmrjhjW3I9moOSEk0XnXW2l4eEYjy hPUhGJBtNl4YCuVU9nxRFU7zSOUC8mvcFgy9WPC/NEXmX0ccdNsdT3b+yY5sYVdB2rvK iGA0DnqfVr4hztdyxTED1Ynlha8t9dx9eTSQm9Z5yoUW6feqkdmV0azyK1YZUI8rUqui aEy2QULHobW8yo+3JZMdLc0b3LVqjKpDTH6XX3SVMGWo6RQpdAJYRQHwQTYooJbGeBOz ffXdRFqfEGJFIaorh9I2ZSH3E8XHoACw5dop5ewtPuqN+4T4mDdTDlphO4bAaoFYCLPY JH1Q== X-Gm-Message-State: AOJu0YzaSUISMztTLAfnp2EsQOd1KhiztVEs7fcZok/PMNiuBWcmiXsH KUo5KHossi2f5IMdb0Lg40/lBbgQgz0UiQcVE3XpU1pqPAuOcJqkZHPeOwJHf6hR1qUHh1OsOSg = X-Google-Smtp-Source: AGHT+IHpajX1cPWw7L4WBg8R1JJS5jcOrZ/t/KeCpYObwVGK16USU3sQrWq8WT9e/odG1bybTN2hjg== X-Received: by 2002:a05:6e02:60a:b0:366:3509:12a0 with SMTP id t10-20020a056e02060a00b00366350912a0mr416861ils.22.1709925953913; Fri, 08 Mar 2024 11:25:53 -0800 (PST) Received: from localhost.localdomain (97-122-82-115.hlrn.qwest.net. [97.122.82.115]) by smtp.gmail.com with ESMTPSA id e94-20020a028667000000b00474f698082bsm30740jai.28.2024.03.08.11.25.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 11:25:53 -0800 (PST) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Set __file__ when source'ing a Python script Date: Fri, 8 Mar 2024 12:25:43 -0700 Message-ID: <20240308192543.3578375-1-tromey@adacore.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org 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. Reviewed-by: Lancelot Six --- 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")