Subject: [PATCH] Fix region deletion behavior in python.el

From: Jakub Ječmínek <[email-masked]>
Date: Mon, 30 Jun 2025 00:19:53 +0200

* lisp/progmodes/python.el (python-indent-dedent-line-backspace):
Check for an active region when deciding whether to perform a
backward delete.  (Bug#48695)

* test/lisp/progmodes/python-tests.el: Add regression test
`python-indent-dedent-line-backspace-4'.
---
 lisp/progmodes/python.el            |  4 +++-
 test/lisp/progmodes/python-tests.el | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index f4f0518dbfd..17fabc322fa 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1970,7 +1970,9 @@ python-indent-dedent-line-backspace
 Argument ARG is passed to `backward-delete-char-untabify' when
 point is not in between the indentation."
   (interactive "*p")
-  (unless (python-indent-dedent-line)
+  (unless (and
+           (not (region-active-p))
+           (python-indent-dedent-line))
     (backward-delete-char-untabify arg)))
 
 (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede)
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index 2c00f47d82c..f13175c9836 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -3780,6 +3780,21 @@ python-indent-dedent-line-backspace-3
                 (pos-bol) (pos-eol))
                "abcdef")))))
 
+(ert-deftest python-indent-dedent-line-backspace-4 ()
+  "Correctly delete region instead of de-indentation.  Bug#48695."
+  (python-tests-with-temp-buffer
+   "
+if True:
+    x ()
+    if False:
+"
+   (python-tests-look-at "if False:")
+   (end-of-line)
+   (set-mark-command nil)
+   (backward-word 2)
+   (call-interactively #'python-indent-dedent-line-backspace)
+   (should (= (current-indentation) 4))))
+
 (ert-deftest python-bob-infloop-avoid ()
   "Test that strings at BOB don't confuse syntax analysis.  Bug#24905"
   (python-tests-with-temp-buffer
-- 
2.49.0