Subject: Re: bug#24856: 25.1.50; Emacs stucks when using C-k to kill lines in python-mode if the next line is a beginning of a docstring

From: [email-masked]
Date: Wed, 02 Nov 2016 22:22:43 -0400

merge 24856 24839
tags 24856 patch
quit

Clément Pit--Claudel <[email-masked]> writes:

>     emacs -Q test.py --eval '(insert " \"")'
>
> In other words, a mis-indented string on the first line is enough to
> reproduce the bug.
>

Yes, seems to be the same as 24839.

> It seems to be an infinite loop in fontification:
>
> Debugger entered--Lisp error: (quit)
>   python-info-beginning-of-statement-p()
>   python-nav--forward-sexp(-1 nil nil)
>   python-nav-forward-sexp(-1 nil nil)
>   python-nav-backward-sexp()
>   python-info-docstring-p((0 nil nil t nil nil 0 nil 5 nil nil))

Here is a patch:

>From 5da4f196fc3c8b411936551568477d70a9046421 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Wed, 2 Nov 2016 21:59:10 -0400
Subject: [PATCH v1] Fix infloop in python docstring detection

The function `python-info-docstring-p' (introduced in 2015-04-05
"python.el: Enhance docstring detection following PEP-257[...]") could
get stuck when called with point before the first expression in the
buffer.  The attempted fix in 2015-04-06 "Fix previous commit to prevent
infloop" did not handle the case where there is only whitespace between
the first expression and the beginning of buffer (Bug#24856, Bug#24839).

* lisp/progmodes/python.el (python-info-docstring-p): Stop looping when
`python-nav-backward-sexp' fails to move point.
---
 lisp/progmodes/python.el | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index e5efc2b..de06efb 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -4867,13 +4867,14 @@ python-info-docstring-p
               2
               (progn
                 (while (save-excursion
-                         (python-nav-backward-sexp)
-                         (setq backward-sexp-point (point))
-                         (and (= indentation (current-indentation))
-                              (not (bobp)) ; Prevent infloop.
-                              (looking-at-p
-                               (concat "[uU]?[rR]?"
-                                       (python-rx string-delimiter)))))
+                         (let ((cur-point (point)))
+                           (python-nav-backward-sexp)
+                           (setq backward-sexp-point (point))
+                           (and (= indentation (current-indentation))
+                                (/= cur-point (point)) ; Prevent infloop.
+                                (looking-at-p
+                                 (concat "[uU]?[rR]?"
+                                         (python-rx string-delimiter))))))
                   ;; Previous sexp was a string, restore point.
                   (goto-char backward-sexp-point)
                   (cl-incf counter))
-- 
2.9.3