GNU bug report logs - #62031
29.0.60; python-mode indentation after re.match

Previous Next

Package: emacs;

Reported by: Rob Moss <robm.dev <at> gmail.com>

Date: Tue, 7 Mar 2023 08:55:01 UTC

Severity: normal

Found in version 29.0.60

Done: Dmitry Gutov <dgutov <at> yandex.ru>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Dmitry Gutov <dgutov <at> yandex.ru>
To: Lele Gaifax <lele <at> metapensiero.it>, 62031 <at> debbugs.gnu.org
Cc: Rob Moss <robm.dev <at> gmail.com>
Subject: bug#62031: 29.0.60; python-mode indentation after re.match
Date: Wed, 8 Mar 2023 19:57:36 +0200
On 08/03/2023 09:53, Lele Gaifax wrote:
> I had a quick glance at this, and while I can confirm the problem with
> the following test
> 
>      diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
>      index 4f24c042c6a..9926c4b002f 100644
>      --- a/test/lisp/progmodes/python-tests.el
>      +++ b/test/lisp/progmodes/python-tests.el
>      @@ -1982,6 +1982,17 @@ python-virt-bin
>          (should (eq (car (python-indent-context)) :after-block-start))
>          (should (= (python-indent-calculate-indentation) 8))))
> 
>      +(ert-deftest python-indent-after-re-match ()
>      +  "Test BUG 62031 regression."
>      +  (python-tests-with-temp-buffer
>      +   "
>      +def test_re(string):
>      +    if re.match('^[a-c]+$', string):
>      +        print('yes')
>      +    else:
>      +"
>      +   (python-tests-look-at "else:")
>      +   (should (= (python-indent-calculate-indentation) 4))))
> 
> I could not figure out how this can be fixed, with my current knowledge
> on python.el.
> 
> There are several places where the regexp produced by `(python-rx
> block-start)' is used, many within a `(looking-at ...)' form and several
> others in a `(re-search-forward ...)': given that block starting
> "keywords" are required to be at the beginning of a line, possibly
> preceded by whitespace, I think that the problem may arise from those
> usages which do not explicitly enforce the constraint, but unfortunately
> all my attempts to do that a) didn't make the test above green and b)
> broke one or more of the other tests.

Right, the problem is that we need some post-check for block-start searches.

python-rx (like rx in general) is just a syntax for Emacs regexp, and it 
doesn't support lookbehind or etc.

After some edebug-ing, the patch below seems to fix this case. Maybe 
other uses of (python-rx block-start) need this treatment as well, but 
I'd like to leave that to someone who comes later. And a lot of them 
happen after a (back-to-indentation) call, so those seem covered too.

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 1f970633bfc..aff8dc206b4 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -5792,7 +5792,9 @@ python-info-dedenter-opening-block-positions
           (catch 'exit
             (while (python-nav--syntactically
                     (lambda ()
-                      (re-search-backward (python-rx block-start) nil t))
+                      (cl-loop do (re-search-backward (python-rx 
block-start) nil t)
+                               until (memq (char-before) '(nil ?\s ?\t 
?\n))
+                               finally return (point)))
                     #'<)
               (let ((indentation (current-indentation)))
                 (when (and (not (memq indentation collected-indentations))





This bug report was last modified 2 years and 134 days ago.

Previous Next


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