From patchwork Mon Mar 4 16:03:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 86748 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 499223858426 for ; Mon, 4 Mar 2024 16:03:59 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 3B25D3858D33 for ; Mon, 4 Mar 2024 16:03:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3B25D3858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3B25D3858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709568211; cv=none; b=m2amCm8k1dxuzQo4A9b5VopWlhAxIx6+t26h0VKbP8u5Iza741zgG7dfXdJrmH6CicxTVFsxFzi1PtBbuYRVhiUBXNyk+YfZH+3DD6jvpnwdtRbun1hQxU8AYqYW7Bo1I7TUbibX8nfkb1jdXGMZf5pZLKork28eRtbgUM5EXkc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709568211; c=relaxed/simple; bh=VNi2BXpVXMuwaLYeiWXwcS4gdWdwarPg1H0klH7Uu00=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-Id:MIME-Version; b=vOGrdlwySkfZUwlqZnZUQ/cCxvU4MvKR3r5/mpxx9OxzzCBChQgUslCiqltoX9NKqtxubUSw3A2TQ1ZDJnV9O0kQqKN7+bjsR5QT3VbYnm3yVCwqQJpaTossIvv7wY6LwsP9DILhvQXHEVxN2SNOgR+WJRchtJwXZIo3BEghojA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 3EAC222B6F for ; Mon, 4 Mar 2024 16:03:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1709568200; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=4FeUVLehhk7oZ/Xu1fzzrWPkv2nkjzRdUql0TMwMxT0=; b=fMAT+72N2fqf39Ex9wC3mnJRhXc40S7xcwuyCRl64OZfD4tLw1UBNjBysPyRQpgn07t80O EEGLRzpEGHavrjYdJjUPPKBJ+jyERBssOWvRg3BaTVaqgAMpi91NT9uo82h4MGY1ZQsTZW qI2KcZlROc8Z8YT3+c5IuTCGp1bk/4o= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1709568200; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=4FeUVLehhk7oZ/Xu1fzzrWPkv2nkjzRdUql0TMwMxT0=; b=rSod3gdkm8nmAV+tu0PcjhjSzlRvRczqBsktMhYLRKFV2mLQbujmhKhKev+y5sqQPeT/pO nMyVTxCFEQmHkADg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1709568200; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=4FeUVLehhk7oZ/Xu1fzzrWPkv2nkjzRdUql0TMwMxT0=; b=fMAT+72N2fqf39Ex9wC3mnJRhXc40S7xcwuyCRl64OZfD4tLw1UBNjBysPyRQpgn07t80O EEGLRzpEGHavrjYdJjUPPKBJ+jyERBssOWvRg3BaTVaqgAMpi91NT9uo82h4MGY1ZQsTZW qI2KcZlROc8Z8YT3+c5IuTCGp1bk/4o= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1709568200; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=4FeUVLehhk7oZ/Xu1fzzrWPkv2nkjzRdUql0TMwMxT0=; b=rSod3gdkm8nmAV+tu0PcjhjSzlRvRczqBsktMhYLRKFV2mLQbujmhKhKev+y5sqQPeT/pO nMyVTxCFEQmHkADg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 2832013A58 for ; Mon, 4 Mar 2024 16:03:20 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id v76OCMjw5WWvdAAAD6G6ig (envelope-from ) for ; Mon, 04 Mar 2024 16:03:20 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH 1/2] [gdb/python] Handle normalized exception returned by PyErr_Fetch Date: Mon, 4 Mar 2024 17:03:46 +0100 Message-Id: <20240304160347.31182-1-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Authentication-Results: smtp-out1.suse.de; none X-Spamd-Result: default: False [1.90 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; R_MISSING_CHARSET(2.50)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; BROKEN_CONTENT_TYPE(1.50)[]; RCPT_COUNT_ONE(0.00)[1]; RCVD_COUNT_THREE(0.00)[3]; TO_DN_NONE(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[sourceware.org:url]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: 1.90 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 With python 3.12 and test-case gdb.python/py-block.exp, before commit a207f6b3a38 ("Rewrite "python" command exception handling"), we had: ... (gdb) python print (block['nonexistent'])^M Traceback (most recent call last):^M File "", line 1, in ^M KeyError: 'nonexistent'^M Error while executing Python code.^M (gdb) PASS: gdb.python/py-block.exp: check nonexistent variable ... but after the commit we have: ... (gdb) python print (block['nonexistent'])^M Python Exception : 'nonexistent'^M Error occurred in Python: 'nonexistent'^M (gdb) FAIL: gdb.python/py-block.exp: check nonexistent variable ... In contrast, with python 3.6 we have: ... (gdb) python print (block['nonexistent'])^M Python Exception : nonexistent^M Error occurred in Python: nonexistent^M (gdb) PASS: gdb.python/py-block.exp: check nonexistent variable ... The change in the test-case is: ... -gdb_test "python print (block\['nonexistent'\])" ".*KeyError: 'nonexistent'.*" \ +gdb_test "python print (block\['nonexistent'\])" ".*KeyError.*: nonexistent.*" \ "check nonexistent variable" ... which drops the single quotes around the nonexistent string, which matches the output with python 3.6, but not with python 3.12. The difference is caused by a difference in the result of PyErr_Fetch. [ PyErr_Fetch is deprecated in python 3.12, this will be addressed in a follow-up commit. ] With python 3.6, we have PyErr_Fetch returning: ... (gdb) p PyObject_Print(error_value, stderr, 0) 'nonexistent'$3 = 0 (gdb) p PyObject_Print(error_value, stderr, 1) nonexistent$4 = 0 ... but with python 3.12, we have instead: ... (gdb) p (int)PyObject_Print(error_value,stderr, 0) KeyError('nonexistent')$3 = 0 (gdb) p (int)PyObject_Print(error_value,stderr, 1) 'nonexistent'$4 = 0 ... In more detail, with python 3.12 we get a normalized exception, so the error_value is an object of class KeyError. With python 3.6, error_value is an unnormalized exception, meaning not of class KeyError. Apparantly we rely here on PyErr_Fetch to return an unnormalized exception. Fix this by pretending to have an unnormalized exception in gdbpy_err_fetch::to_string. Tested on aarch64-linux. PR python/31425 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31425 --- gdb/python/py-utils.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) base-commit: 1485a3fb63619cced99dd7a4a043cf01a0f423d9 diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index 9382eb62a5f..8eee33f89bb 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -196,7 +196,19 @@ gdbpy_err_fetch::to_string () const gdb.GdbError ("message"). */ if (m_error_value.get () != nullptr && m_error_value.get () != Py_None) - return gdbpy_obj_to_string (m_error_value.get ()); + { + if ((PyObject *)Py_TYPE (m_error_value.get ()) == m_error_type.get ()) + { + /* Detected a normalized exception. */ + PyObject *args = PyException_GetArgs (m_error_value.get ()); + if (PyTuple_Size (args) == 1) + { + /* Pretend to be looking at an unnormalized exception. */ + return gdbpy_obj_to_string (PyTuple_GetItem (args, 0)); + } + } + return gdbpy_obj_to_string (m_error_value.get ()); + } else return gdbpy_obj_to_string (m_error_type.get ()); }