Subject: Re: bug#74738: 31.0.50; Freezes in Python-mode on some Python file when searching or scrolling

From: kobarity <[email-masked]>
Date: Tue, 10 Dec 2024 00:04:28 +0900

rehan malak wrote:
> Please tell me if there is a workaround without losing python syntax color ?

If you can configure Emacs to use tree-sitter, python-ts-mode would
not be affected by this problem.

Eli Zaretskii wrote:
> 
> > From: rehan malak <[email-masked]>
> > Date: Sun, 8 Dec 2024 14:05:36 +0100
> > 
> > I can reproduce systematically this freeze dealing with a 10000lines
> > Python file :
> > 
> > wget https://raw.githubusercontent.com/hugsy/gef/refs/heads/main/gef.py
> > emacs -Q gef.py

It seems to be reproducible after the following commit:

commit 3b3274a85c2f5df21d76d82e0d7740005aa84fdf
Author: Stefan Monnier <[email-masked]>
Date:   Fri Oct 16 14:03:59 2020 -0400

    * lisp/progmodes/python.el: Teach f-strings to `font-lock`

    (python--f-string-p, python--font-lock-f-strings): New functions.
    (python-font-lock-keywords-maximum-decoration): Use them.

> A much smaller reproducer is attached below.  Just visiting it freezes
> Emacs.  But if I change the line marked below:
> 
>     @classmethod
>     def is_valid(cls, _: pathlib.Path) -> bool:
>         raise NotImplementedError
> 
>     def __str__(self) -> str:
>   >>>>> return f"{self.name}('{self.path.absolute()}', entry @ {self.entry_point:#x})"
> 
> to say this instead:
> 
>         return self.name
> 
> the problem goes away.  So it is something in that complex expression
> that trips syntax-ppss.
> 
> Stefan and kobarity, any suggestions or ideas?

I could not reproduce the freeze, but it took several seconds to visit
the reproducer.  It appears to be possible to work around this by
simply removing the format specifier ":#x".

return f"{self.name}('{self.path.absolute()}', entry @ {self.entry_point})"

So there may be a problem with font-lock when there is an f-string
format specifier, but I have not found the root cause yet.