unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#65870: 30.0.50; Error in python-mode when try to delete a character in badly-indented code
@ 2023-09-11 16:36 Daniel Lopez
  2023-09-16 10:05 ` Eli Zaretskii
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Lopez @ 2023-09-11 16:36 UTC (permalink / raw)
  To: 65870

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.

Daniel



In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
  3.24.36, cairo version 1.17.8) of 2023-02-21 built on ENO
Repository revision: 098add06eb7d742ff0e6ee0e294931a93a60c71c
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101008
System Description: Arch Linux

Configured using:
  'configure --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib
  --localstatedir=/var --mandir=/usr/share/man --with-gameuser=:games
  --with-modules --without-libotf --without-m17n-flt --without-gconf
  --with-native-compilation=yes --with-xinput2 --with-x-toolkit=gtk3
  --without-xaw3d --with-sound=no --with-tree-sitter --without-gpm
  --without-compress-install
  '--program-transform-name=s/\([ec]tags\)/\1.emacs/'
  'CFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions
  -Wp,-D_FORTIFY_SOURCE=2 -Wformat -fstack-clash-protection
  -fcf-protection'
  LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now'

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

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

Major mode: Python

Minor modes in effect:
   tooltip-mode: t
   global-eldoc-mode: t
   eldoc-mode: t
   show-paren-mode: t
   electric-indent-mode: t
   mouse-wheel-mode: t
   tool-bar-mode: t
   menu-bar-mode: t
   file-name-shadow-mode: t
   global-font-lock-mode: t
   font-lock-mode: t
   blink-cursor-mode: t
   line-number-mode: t
   transient-mark-mode: t
   auto-composition-mode: t
   auto-encryption-mode: t
   auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search time-date mm-decode mm-bodies
mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail
rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils help-fns
radix-tree cl-print debug backtrace find-func python project byte-opt
pcase treesit comint ansi-osc ring cl-loaddefs comp comp-cstr warnings
icons subr-x rx cl-seq cl-macs gv cl-extra help-mode bytecomp
byte-compile cl-lib ansi-color cus-start cus-load rmc iso-transl tooltip
cconv eldoc paren electric uniquify ediff-hook vc-hooks lisp-float-type
elisp-mode mwheel term/x-win x-win term/common-win x-dnd tool-bar dnd
fontset image regexp-opt fringe tabulated-list replace newcomment
text-mode lisp-mode prog-mode register page tab-bar menu-bar rfn-eshadow
isearch easymenu timer select scroll-bar mouse jit-lock font-lock syntax
font-core term/tty-colors frame minibuffer nadvice seq simple cl-generic
indonesian philippine cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
composite emoji-zwj charscript charprop case-table epa-hook
jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button loaddefs
theme-loaddefs faces cus-face macroexp files window text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget keymap
hashtable-print-readable backquote threads dbusbind inotify lcms2
dynamic-setting system-font-setting font-render-setting cairo
move-toolbar gtk x-toolkit xinput2 x multi-tty make-network-process
native-compile emacs)

Memory information:
((conses 16 105838 7866)
  (symbols 48 9884 0)
  (strings 32 26714 1992)
  (string-bytes 1 822994)
  (vectors 16 18667)
  (vector-slots 8 375164 12614)
  (floats 8 37 45)
  (intervals 56 310 0)
  (buffers 984 13))





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#65870: 30.0.50; Error in python-mode when try to delete a character in badly-indented code
  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
  0 siblings, 1 reply; 4+ messages in thread
From: Eli Zaretskii @ 2023-09-16 10:05 UTC (permalink / raw)
  To: Daniel Lopez, kobarity; +Cc: 65870

> 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?





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#65870: 30.0.50; Error in python-mode when try to delete a character in badly-indented code
  2023-09-16 10:05 ` Eli Zaretskii
@ 2023-09-16 15:06   ` kobarity
  2023-09-17  4:06     ` Daniel Lopez
  0 siblings, 1 reply; 4+ messages in thread
From: kobarity @ 2023-09-16 15:06 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Daniel Lopez, 65870

[-- 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* bug#65870: 30.0.50; Error in python-mode when try to delete a character in badly-indented code
  2023-09-16 15:06   ` kobarity
@ 2023-09-17  4:06     ` Daniel Lopez
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Lopez @ 2023-09-17  4:06 UTC (permalink / raw)
  To: kobarity, Eli Zaretskii; +Cc: 65870

On 16/09/2023 16:06, kobarity wrote:
> 
> 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.

On applying the fix to my own installation, I can confirm that it works 
for me. Thanks. :)





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-09-17  4:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2023-09-17  4:06     ` Daniel Lopez

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).