From patchwork Thu Nov 16 14:55:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 80041 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 89F883858C20 for ; Thu, 16 Nov 2023 14:55:43 +0000 (GMT) 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.129.124]) by sourceware.org (Postfix) with ESMTPS id D9DD13858D33 for ; Thu, 16 Nov 2023 14:55:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D9DD13858D33 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 D9DD13858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700146530; cv=none; b=H2bXbRYZ5ZskFWn9JVLNcqIcemfbuJZ1ne+OTdRp/1poF3utLBigjProW1cfxg34xCgzB3VJnHnY7lGiUzTxpHtNuVT4GdJhExDhuKFOf7hBk7zd1GaFI/stP6hW/a1UPMFldYPkWzlLcbC+aTyx0egh+3d0b8k9mcoYXUZGOJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700146530; c=relaxed/simple; bh=6nqHruTzZZ8OBWbm+ISojJYjAxl4dkjVcAtSIn7qo1U=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Asqr+kDjxyyCdo42Flp+VWlj2mzfTMFi8nV2M25KGWvn8otzxKH2fcbVp3zS4l2OaWfVphNjza/P98u7awsw11QnpFxb0ZWexeu8aF6mLph2I/QTWiMfmK+0JuPXwXgfPfbGw//+YSBQIx2UqLyQz4YHPJXYoGBNsFjXUd2P8jw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700146528; 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; bh=66lyx3+TM70+JKzG2Dzyijbgd+c/cvXpyFxz1OowMfw=; b=gu65OorKOjWIUV6fy8CFOXpikBi5NBCZzvhi3UGXtq58ggrY0jkTFr9wFZSi1HaQdI4rWI aJG7+/TDPllHb6K+vxigvGxysNENu5EgebQMMS+Jrq6guS8RsFlZXFaR+v+c3As0/hiat2 HUu+vuRwz5E2uRZCkYhdEuVyBxay0U4= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-363-bvKYGPdvPmqqfFeFuRchcg-1; Thu, 16 Nov 2023 09:55:27 -0500 X-MC-Unique: bvKYGPdvPmqqfFeFuRchcg-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-32f7cf9c33bso449005f8f.3 for ; Thu, 16 Nov 2023 06:55:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700146525; x=1700751325; 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=66lyx3+TM70+JKzG2Dzyijbgd+c/cvXpyFxz1OowMfw=; b=kOWhQkwm3qjRIlKy/eXlRVkqr1iA3vqmmIQBBOleVmSZo/S8wc0M0SyyyfeCvtQrue ZNal9NYsD50eIfYq1Yju9FP+tJrjVK66kAO1spOlb7JR7CWyvSmQX5nrGSyuhfVugeL6 lui5juZPPKF+W7WFtyHGg3pELy6u22dzoLytnadhvSEPyPOnWKu/Ov4sSgY2bZIvGntI LCUjK5hkoQIdLb2dXmpigvfdsYYBj2GLiVTpkf2qlWv3BtgUFLoT6H8APwfc/YkqUkfa 7kQOt0Fg71nOn4AVALkndXlNuSsAgw0iWAPOr+DuRXjzXbnkegoeWmeRBhCk3urHDl1b sKYw== X-Gm-Message-State: AOJu0YxINl3R9n0Vi9GtnkmliJy8GpWlumCYAQZplZvtTrl/4jiE6BmO HByOnyWGnDbR8rtVaX9gqsthgRrAPrjTxbR+eML/YtFC0W64Z2pZNddu6RJzMt0AnYqaHO78elN HSI8Lu0Ix9eoCESCi5Xy1xxdJhN/NwoIR5aCJiBTKVyvS57dKbd3B0XUv1BimR5kQRjRnA6kO4o OtFAes3A== X-Received: by 2002:a5d:47ac:0:b0:331:3b7e:8032 with SMTP id 12-20020a5d47ac000000b003313b7e8032mr2395016wrb.49.1700146525728; Thu, 16 Nov 2023 06:55:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IGHQfj0ROlFkEStqnzZVE/v9EBXFCvKot4tr4CCYAJ6KGIY0KOkW9DovAdqqQVlqUtHejameg== X-Received: by 2002:a5d:47ac:0:b0:331:3b7e:8032 with SMTP id 12-20020a5d47ac000000b003313b7e8032mr2394998wrb.49.1700146525329; Thu, 16 Nov 2023 06:55:25 -0800 (PST) Received: from localhost (105.226.159.143.dyn.plus.net. [143.159.226.105]) by smtp.gmail.com with ESMTPSA id i18-20020a5d6312000000b0032d9a1f2ec3sm13925590wru.27.2023.11.16.06.55.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Nov 2023 06:55:24 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH] gdb/python: handle completion returning a non-sequence Date: Thu, 16 Nov 2023 14:55:22 +0000 Message-Id: <87fa2a8161452cf2eb17ca7ba561831eebe4f254.1700146245.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 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 GDB's Python API documentation for gdb.Command.complete() says: The 'complete' method can return several values: * If the return value is a sequence, the contents of the sequence are used as the completions. It is up to 'complete' to ensure that the contents actually do complete the word. A zero-length sequence is allowed, it means that there were no completions available. Only string elements of the sequence are used; other elements in the sequence are ignored. * If the return value is one of the 'COMPLETE_' constants defined below, then the corresponding GDB-internal completion function is invoked, and its result is used. * All other results are treated as though there were no available completions. So, returning a non-sequence, and non-integer from a complete method should be fine; it should just be treated as though there are no completions. However, if I write a complete method that returns None, I see this behaviour: (gdb) complete completefilenone x Python Exception : 'NoneType' object is not iterable warning: internal error: Unhandled Python exception (gdb) Which is caused because we are currently missing a call to PyErr_Clear(). Fixed in this commit, and added a test. I've also added a test in which the complete() function raises an exception. This case was already handled (again, we treat this as if there are no completions), but I don't believe this case was being tested ... it is now. --- gdb/python/py-cmd.c | 6 +++++- gdb/testsuite/gdb.python/py-completion.exp | 7 +++++++ gdb/testsuite/gdb.python/py-completion.py | 24 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) base-commit: 8d081332318b07f1a2b4af0fdfbdc256e37dca93 diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index 20a384d6907..a906766c381 100644 --- a/gdb/python/py-cmd.c +++ b/gdb/python/py-cmd.c @@ -295,7 +295,11 @@ cmdpy_completer (struct cmd_list_element *command, gdbpy_ref<> iter (PyObject_GetIter (resultobj.get ())); if (iter == NULL) - return; + { + /* Ignore any errors, don't offer any completions. */ + PyErr_Clear (); + return; + } bool got_matches = false; while (true) diff --git a/gdb/testsuite/gdb.python/py-completion.exp b/gdb/testsuite/gdb.python/py-completion.exp index 23f981e944a..6264df0f291 100644 --- a/gdb/testsuite/gdb.python/py-completion.exp +++ b/gdb/testsuite/gdb.python/py-completion.exp @@ -46,6 +46,13 @@ if { [readline_is_used] && ![is_remote host] } { # Just discarding whatever we typed. gdb_test " " ".*" "discard #[incr discard]" + # These should offer no suggestions - the complete() methods + # either return a non-sequence or raise an exception. + foreach_with_prefix cmd { completefilenone completefileexception} { + gdb_test_no_output "complete $cmd ${testdir_complete}" \ + "no suggestions given" + } + # This is the problematic one. send_gdb "completefilemethod ${testdir_complete}\t" gdb_test_multiple "" "completefilemethod completion" { diff --git a/gdb/testsuite/gdb.python/py-completion.py b/gdb/testsuite/gdb.python/py-completion.py index abec06921c0..19628cbcceb 100644 --- a/gdb/testsuite/gdb.python/py-completion.py +++ b/gdb/testsuite/gdb.python/py-completion.py @@ -28,6 +28,28 @@ class CompleteFileInit(gdb.Command): raise gdb.GdbError("not implemented") +class CompleteFileNone(gdb.Command): + def __init__(self): + gdb.Command.__init__(self, "completefilenone", gdb.COMMAND_USER) + + def invoke(self, argument, from_tty): + raise gdb.GdbError("not implemented") + + def complete(self, text, word): + return None + + +class CompleteFileException(gdb.Command): + def __init__(self): + gdb.Command.__init__(self, "completefileexception", gdb.COMMAND_USER) + + def invoke(self, argument, from_tty): + raise gdb.GdbError("not implemented") + + def complete(self, text, word): + raise gdb.GdbError("failed completion") + + class CompleteFileMethod(gdb.Command): def __init__(self): gdb.Command.__init__(self, "completefilemethod", gdb.COMMAND_USER) @@ -203,6 +225,8 @@ class CompleteLimit7(gdb.Command): CompleteFileInit() +CompleteFileNone() +CompleteFileException() CompleteFileMethod() CompleteFileCommandCond() CompleteLimit1()