GNU bug report logs - #77941
[PATCH] Fix Python block end predicates

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Reported by: kobarity <kobarity@HIDDEN>; Keywords: patch; Done: Eli Zaretskii <eliz@HIDDEN>; Maintainer for emacs is bug-gnu-emacs@HIDDEN.

Message received at 77941-done <at> debbugs.gnu.org:


Received: (at 77941-done) by debbugs.gnu.org; 26 Apr 2025 11:23:50 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sat Apr 26 07:23:50 2025
Received: from localhost ([127.0.0.1]:58744 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1u8ddO-000155-1d
	for submit <at> debbugs.gnu.org; Sat, 26 Apr 2025 07:23:50 -0400
Received: from eggs.gnu.org ([2001:470:142:3::10]:57286)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <eliz@HIDDEN>) id 1u8ddM-00014n-6d
 for 77941-done <at> debbugs.gnu.org; Sat, 26 Apr 2025 07:23:48 -0400
Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@HIDDEN>)
 id 1u8ddG-000767-T7; Sat, 26 Apr 2025 07:23:42 -0400
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date:
 mime-version; bh=65fnicg5JtL+zjCqIWvHvVJ4v/QnMeIhuCTaaFGucnY=; b=hlPYRuPgdnMo
 G3a8yMbO78wkvvHGF+24sXLPw5YoHc8BXC+aQAdI5snduLD+z4uTOba3XGoDVVn+axXCJ9qoryPfl
 E1LJnDXXi8VV6boDY8QCKbPlAMNnXRKiWpj2/ucLS/rU02XHcd4xcP6SClSUnblL+08BvtclcxWZ6
 hgL0u5uOKKa/bxjvYoPZ6e4GthoCHWx07qazTAysFywd9+5SKN5RF5NBEspzEF83SY1AzY1p+HtBL
 NhYRga42/euRijjJkyrp5LaEubqXhZnT0dmB2GJJYbvPfGbMM4kyr9B2zg8nnu2tI3+ty+Ha0eQPx
 283jjQW8/sEkKeAzZ8ye4Q==;
Date: Sat, 26 Apr 2025 14:23:40 +0300
Message-Id: <86h62bupxf.fsf@HIDDEN>
From: Eli Zaretskii <eliz@HIDDEN>
To: kobarity <kobarity@HIDDEN>
In-Reply-To: <eke75xiyyj8i.wl-kobarity@HIDDEN> (message from kobarity on
 Sun, 20 Apr 2025 23:56:29 +0900)
Subject: Re: bug#77941: [PATCH] Fix Python block end predicates
References: <eke75xiyyj8i.wl-kobarity@HIDDEN>
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 77941-done
Cc: 77941-done <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

> Date: Sun, 20 Apr 2025 23:56:29 +0900
> From: kobarity <kobarity@HIDDEN>
> 
> In the process of looking into #77620, I noticed bugs in the
> end-of-block detection functions in python.el.  If there is a comment
> on the last line of the block, `python-info-statement-ends-block-p'
> and `python-info-end-of-block-p' will not work correctly. 
> 
> Try the following Python code.
> 
> def func():
>     return 0  # Comment
> 
> When the point is located after "0" of the return statement, both
> `python-info-statement-ends-block-p' and `python-info-end-of-block-p'
> return nil.  If there is no comment on the line, they return t as
> expected.
> 
> This bug affects `python-nav-forward-sexp'.  In the above example, if
> the point is located at the beginning of "def func()" line,
> (python-nav-forward-sexp) moves the point to after "0".  This is the
> expected behavior.
> 
> However, if we execute (python-nav-forward-sexp -1) there, it moves
> the point to "0". This is not the expected behavior.  If there is no
> comment, it moves the point to the beginning of "def func()" line.
> 
> Attached is a patch that fixes this problem.

Thanks, installed, and closing the bug.




Notification sent to kobarity <kobarity@HIDDEN>:
bug acknowledged by developer. Full text available.
Reply sent to Eli Zaretskii <eliz@HIDDEN>:
You have taken responsibility. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 20 Apr 2025 14:56:47 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sun Apr 20 10:56:46 2025
Received: from localhost ([127.0.0.1]:50517 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1u6W6A-0004aG-3I
	for submit <at> debbugs.gnu.org; Sun, 20 Apr 2025 10:56:46 -0400
Received: from lists.gnu.org ([2001:470:142::17]:58736)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <kobarity@HIDDEN>)
 id 1u6W67-0004Z9-Ec
 for submit <at> debbugs.gnu.org; Sun, 20 Apr 2025 10:56:44 -0400
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <kobarity@HIDDEN>)
 id 1u6W60-0008Ei-RP
 for bug-gnu-emacs@HIDDEN; Sun, 20 Apr 2025 10:56:36 -0400
Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <kobarity@HIDDEN>)
 id 1u6W5z-00020i-71
 for bug-gnu-emacs@HIDDEN; Sun, 20 Apr 2025 10:56:36 -0400
Received: by mail-pf1-x42e.google.com with SMTP id
 d2e1a72fcca58-7398d65476eso2576999b3a.1
 for <bug-gnu-emacs@HIDDEN>; Sun, 20 Apr 2025 07:56:34 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1745160993; x=1745765793; darn=gnu.org;
 h=mime-version:user-agent:subject:to:from:message-id:date:from:to:cc
 :subject:date:message-id:reply-to;
 bh=XWXddzKMeUIezYuZyaYa3Sj9QGsnFZA7pgLpzT2mBoU=;
 b=imcGiuHR9RlaBNzKgtr6/rvEIl7W8kV4YJ64rG5BaXeZciNrt0kDGYzIhjP+zkLwVY
 RwF1QbxGYhHIL17hKk3quQ1ToUQRVWf91UM1T4KPG3Cr7MnJmtnVWKHXOxmXrm5ZwAtE
 TmAAWvCgimNinNh6H4rGqLBl7UB8fjMr9Vj7fmunBBlgwXY9C3pPcY7anAshVuPzIPBl
 v9f6g/wKxwjc3PeR5KB22dZjeD61HKdJRj5m2ueIjPKGj2SKKuNI5p8Strpo6akPsbi6
 1uASlz7JB2X/KvUzp3jAVvxHvQiYZsj+2F0fCYe5qW+KdJKXUxIsToNZbQhFW+YaHsuo
 4ryQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1745160993; x=1745765793;
 h=mime-version:user-agent:subject:to:from:message-id:date
 :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=XWXddzKMeUIezYuZyaY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cca58-73dbe4e6458mr14168557b3a.3.1745160992996; 
 Sun, 20 Apr 2025 07:56:32 -0700 (PDT)
Received: from localhost (58x12x133x161.ap58.ftth.ucom.ne.jp. [58.12.133.161])
 by smtp.gmail.com with ESMTPSA id
 d2e1a72fcca58-73dbf8e0c09sm4828498b3a.38.2025.04.20.07.56.31
 for <bug-gnu-emacs@HIDDEN>
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Sun, 20 Apr 2025 07:56:32 -0700 (PDT)
Date: Sun, 20 Apr 2025 23:56:29 +0900
Message-ID: <eke75xiyyj8i.wl-kobarity@HIDDEN>
From: kobarity <kobarity@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: [PATCH] Fix Python block end predicates
User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue)
 FLIM-LB/1.14.9 (=?ISO-8859-4?Q?Goj=F2?=) APEL-LB/10.8 EasyPG/1.0.0
 Emacs/31.0.50 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO)
MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue")
Content-Type: multipart/mixed; boundary="Multipart_Sun_Apr_20_23:56:29_2025-1"
Received-SPF: pass client-ip=2607:f8b0:4864:20::42e;
 envelope-from=kobarity@HIDDEN; helo=mail-pf1-x42e.google.com
X-Spam_score_int: -20
X-Spam_score: -2.1
X-Spam_bar: --
X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001,
 RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 1.0 (+)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.0 (/)

--Multipart_Sun_Apr_20_23:56:29_2025-1
Content-Type: text/plain; charset=US-ASCII


In the process of looking into #77620, I noticed bugs in the
end-of-block detection functions in python.el.  If there is a comment
on the last line of the block, `python-info-statement-ends-block-p'
and `python-info-end-of-block-p' will not work correctly. 

Try the following Python code.

def func():
    return 0  # Comment

When the point is located after "0" of the return statement, both
`python-info-statement-ends-block-p' and `python-info-end-of-block-p'
return nil.  If there is no comment on the line, they return t as
expected.

This bug affects `python-nav-forward-sexp'.  In the above example, if
the point is located at the beginning of "def func()" line,
(python-nav-forward-sexp) moves the point to after "0".  This is the
expected behavior.

However, if we execute (python-nav-forward-sexp -1) there, it moves
the point to "0". This is not the expected behavior.  If there is no
comment, it moves the point to the beginning of "def func()" line.

Attached is a patch that fixes this problem.

--
In GNU Emacs 31.0.50 (build 8, x86_64-pc-linux-gnu, GTK+ Version
 3.24.41, cairo version 1.18.0) of 2025-01-01 built on ubuntu2404
Repository revision: 78bc5949db489b143f7424540d6cc56fc529c9ea
Repository branch: master
System Description: Ubuntu 24.04.2 LTS

Configured using:
 'configure --with-pgtk --with-native-compilation --with-tree-sitter'

--Multipart_Sun_Apr_20_23:56:29_2025-1
Content-Type: application/octet-stream; type=patch; name="0001-Fix-Python-block-end-predicates.patch"
Content-Disposition: attachment; filename="0001-Fix-Python-block-end-predicates.patch"
Content-Transfer-Encoding: 7bit

From 3056104cb0b5599bf11e7764a4c9138bf0ef2c62 Mon Sep 17 00:00:00 2001
From: kobarity <kobarity@HIDDEN>
Date: Sun, 20 Apr 2025 21:14:46 +0900
Subject: [PATCH] Fix Python block end predicates

* lisp/progmodes/python.el
(python-info-statement-ends-block-p),
(python-info-end-of-block-p): Add consideration of comments.
* test/lisp/progmodes/python-tests.el
(python-info-statement-ends-block-p-3),
(python-info-end-of-block-p-3): New tests.
---
 lisp/progmodes/python.el            |  9 +++++++--
 test/lisp/progmodes/python-tests.el | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 32035773fde..b03e4f9efdf 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -6159,7 +6159,9 @@ python-info-statement-ends-block-p
   (let ((end-of-block-pos (save-excursion
                             (python-nav-end-of-block)))
         (end-of-statement-pos (save-excursion
-                                (python-nav-end-of-statement))))
+                                (python-nav-end-of-statement)
+                                (python-util-forward-comment -1)
+                                (point))))
     (and end-of-block-pos end-of-statement-pos
          (= end-of-block-pos end-of-statement-pos))))
 
@@ -6182,7 +6184,10 @@ python-info-beginning-of-block-p
 
 (defun python-info-end-of-block-p ()
   "Return non-nil if point is at end of block."
-  (and (python-info-end-of-statement-p)
+  (and (= (point) (save-excursion
+                    (python-nav-end-of-statement)
+                    (python-util-forward-comment -1)
+                    (point)))
        (python-info-statement-ends-block-p)))
 
 (define-obsolete-function-alias
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index 898e2b036e0..22a7c3a5e89 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -5821,6 +5821,15 @@ python-info-statement-ends-block-p-2
    (python-tests-look-at "raise ValueError(")
    (should (python-info-statement-ends-block-p))))
 
+(ert-deftest python-info-statement-ends-block-p-3 ()
+  (python-tests-with-temp-buffer
+   "
+def function():
+    print()  # Comment
+"
+   (python-tests-look-at "print()")
+   (should (python-info-statement-ends-block-p))))
+
 (ert-deftest python-info-beginning-of-statement-p-1 ()
   (python-tests-with-temp-buffer
    "
@@ -5983,6 +5992,15 @@ python-info-end-of-block-p-2
    (python-util-forward-comment -1)
    (should (python-info-end-of-block-p))))
 
+(ert-deftest python-info-end-of-block-p-3 ()
+  (python-tests-with-temp-buffer
+   "
+def function():
+    print()  # Comment
+"
+   (python-tests-look-at "  # Comment")
+   (should (python-info-end-of-block-p))))
+
 (ert-deftest python-info-dedenter-opening-block-position-1 ()
   (python-tests-with-temp-buffer
       "
-- 
2.43.0


--Multipart_Sun_Apr_20_23:56:29_2025-1--




Acknowledgement sent to kobarity <kobarity@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to bug-gnu-emacs@HIDDEN:
bug#77941; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Sat, 26 Apr 2025 11:30:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.