unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: kobarity <kobarity@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Daniel Lopez <daniel.lopez999@gmail.com>, 65870@debbugs.gnu.org
Subject: bug#65870: 30.0.50; Error in python-mode when try to delete a character in badly-indented code
Date: Sun, 17 Sep 2023 00:06:58 +0900	[thread overview]
Message-ID: <eke7msxmxilp.wl-kobarity@gmail.com> (raw)
In-Reply-To: <83led6igai.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1465 bytes --]


Eli Zaretskii wrote:
> > Date: Mon, 11 Sep 2023 17:36:59 +0100
> > From: Daniel Lopez <daniel.lopez999@gmail.com>
> > 
> > To reproduce from emacs -Q:
> >   C-x b hello.py RET
> >   M-x python-mode
> >   On the first line, type the word "return", then press the Return key.
> >   On the second line, type one or more spaces.
> >   Press Backspace. This triggers the error message:
> >       python-indent-line: Wrong type argument: wholenump, -4
> >   and, the last-typed space is not deleted.
> > 
> > Backtrace of the error:
> >   Debugger entered--Lisp error: (wrong-type-argument wholenump -4)
> >     indent-line-to(-4)
> >     python-indent-line(t)
> >     python-indent-dedent-line()
> >     python-indent-dedent-line-backspace(1)
> >     funcall-interactively(python-indent-dedent-line-backspace 1)
> >     command-execute(python-indent-dedent-line-backspace)
> > 
> > This can also happen with Python keywords/statements that tend to end a
> > scope, eg. "break" or "raise", as well as "return".
> > 
> > I guess the smart dedenting sees the "return" (or similar statement) at
> > top-level scope and then tries to move the cursor to a parent scope /
> > negative column that doesn't exist. Perhaps one of those indent
> > functions could clamp itself somehow to not do that but I'm not sure
> > where the best place for that would be.
> 
> Thanks.
> 
> kobarity, any suggestions or comments?

Thank you for the report.  The attached is the proposed fix.

[-- Attachment #2: 0001-Fix-Python-mode-error-caused-by-incorrect-indentatio.patch --]
[-- Type: application/octet-stream, Size: 1810 bytes --]

From 32f0dbe5f942ffa60f0945d2ffb771db82db5832 Mon Sep 17 00:00:00 2001
From: kobarity <kobarity@gmail.com>
Date: Sat, 16 Sep 2023 23:14:45 +0900
Subject: [PATCH] Fix Python mode error caused by incorrect indentation

* lisp/progmodes/python.el (python-indent--calculate-indentation):
Guard against negative indentation.  (Bug #65870)

* test/lisp/progmodes/python-tests.el
(python-indent-badly-indented-block-end): New test.
---
 lisp/progmodes/python.el            | 2 +-
 test/lisp/progmodes/python-tests.el | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 4b940b3f13b..d7e49e225d8 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1660,7 +1660,7 @@ python-indent--calculate-indentation
         (`(:after-block-end . ,start)
          ;; Subtract one indentation level.
          (goto-char start)
-         (- (current-indentation) python-indent-offset))
+         (max 0 (- (current-indentation) python-indent-offset)))
         (`(:at-dedenter-block-start . ,_)
          ;; List all possible indentation levels from opening blocks.
          (let ((opening-block-start-points
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index a44a11896f0..f3456030975 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -2081,6 +2081,15 @@ python-indent-after-bare-match
    (python-tests-look-at "else:")
    (should (= (python-indent-calculate-indentation) 4))))
 
+(ert-deftest python-indent-badly-indented-block-end ()
+  "Test BUG 65870 regression."
+  (python-tests-with-temp-buffer
+   "
+return
+"
+   (goto-char (point-max))
+   (should (= (python-indent-calculate-indentation) 0))))
+
 \f
 ;;; Filling
 
-- 
2.34.1


  reply	other threads:[~2023-09-16 15:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-11 16:36 bug#65870: 30.0.50; Error in python-mode when try to delete a character in badly-indented code Daniel Lopez
2023-09-16 10:05 ` Eli Zaretskii
2023-09-16 15:06   ` kobarity [this message]
2023-09-17  4:06     ` Daniel Lopez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eke7msxmxilp.wl-kobarity@gmail.com \
    --to=kobarity@gmail.com \
    --cc=65870@debbugs.gnu.org \
    --cc=daniel.lopez999@gmail.com \
    --cc=eliz@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).