Subject: bug#56742: 29.0.50; Python indendation problem after comment

From: Stephen Berman <[email-masked]>
Date: Sun, 24 Jul 2022 17:42:58 +0200

0. emacs -Q
1. C-x b test RET
2. Insert the following into buffer "test":

def test(n):
    if n < 0:
        return -1
    # test
    else:
        return 0

2. M-x python-mode RET (with buffer "test" current)
3. Put point on the line containing "else:" and type TAB
=> "else:" is dedented to column 0 and remains there on typing TAB
again (and again).

If the line containing "# test" is deleted and point put back on the
line containing "else:", then typing TAB indents "else:" to column 4
(where it was before being wrongly dedented) and remains there on typing
TAB again (as it should).

The same behavior happens with "elif" lines preceded by a comment line.

The attached patch prevents the wrong dedenting, but I'm not familiar
enough with the python.el code to be confident it's the correct fix.

In GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, cairo version 1.17.6)
 of 2022-07-22 built on strobelfs2
Repository revision: 8434396fa362c6644ff23e1f48acace7b85ffe8f
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101003
System Description: Linux From Scratch r11.0-165

Configured using:
 'configure --with-xinput2 --with-xwidgets 'CFLAGS=-Og -g3'
 PKG_CONFIG_PATH=/opt/qt5/lib/pkgconfig'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
JSON LCMS2 LIBSYSTEMD LIBXML2 MODULES NOTIFY INOTIFY PDUMPER PNG RSVG
SECCOMP SOUND SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS WEBP X11 XDBE XIM
XINPUT2 XPM XWIDGETS GTK3 ZLIB

Important settings:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index f31832fec9..01b6693f94 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -5168,7 +5168,8 @@ python-info-dedenter-opening-block-positions
                                  (while (and (< (point) cur-line)
                                              (setq no-back-indent
                                                    (or (> (current-indentation) indentation)
-                                                       (python-info-current-line-empty-p))))
+                                                       (python-info-current-line-empty-p)
+                                                       (python-info-current-line-comment-p))))
                                    (forward-line)))
                                no-back-indent)))
                   (setq collected-indentations