Subject: Re: bug#77853: 30.1; python-inferior-mode: completion regression

From: Liu Hui <[email-masked]>
Date: Fri, 18 Apr 2025 13:38:04 +0800

On Thu, Apr 17, 2025 at 2:29 PM Eli Zaretskii <[email-masked]> wrote:
>
> > From: Christian Sattler <[email-masked]>
> > Date: Wed, 16 Apr 2025 21:59:01 +0200
> >
> > Completing a non-existing prefix attempts to complete it as a filename.
> >
> > Steps to reproduce:
> >
> > 1. touch test.file
> > 2. emacs -Q
> > 3. M-x run-python
> > 4. test
> > 5. M-x python-shell-completion-complete-or-indent
> >
> > Outcome: incorrectly completes to test.file.
> >
> > This regression was introduced in commit 0b9c714. The function
> > python-shell-completion-at-point now returns nil instead of an empty
> > list of candidates. This causes the next element of
> > completion-at-point-functions list to be tried. This is
> > comint-completion-at-point and completes filenames, which does not
> > make sense in this context.
>
> Thanks.
>
> Liu Hui and kobarity, any suggestions for how to fix that?  The fix
> should be safe enough for the release branch, since the regression was
> introduced by Emacs 30.1.

We can just remove comint-filename-completion from
comint-dynamic-complete-functions, which is called by
comint-completion-at-point in completion-at-point-functions.

Before commit 0b9c714, comint-filename-completion was actually not
used due to python-shell-completion-at-point. Therefore, removing it
is safe and acceptable.

Users who need proper filename completion in Python shell can use
specialized completion backends, e.g.

1. touch test.file
2. export PYTHONSTARTUP="$(python -m jedi repl)"; emacs -Q
3. M-x run-python
4. test<tab>         => no completion
5. open("test<tab>   => open("test.file"
From f20dc7213de702f293d1580494a28ce4d795c17d Mon Sep 17 00:00:00 2001
From: Liu Hui <[email-masked]>
Date: Fri, 18 Apr 2025 13:32:01 +0800
Subject: [PATCH] Fix filename completion in Python shell (bug#77853)

comint-filename-completion may complete the filename at wrong
locations. Users who need proper filename completion should use
specialized completion backends (e.g. Jedi).

* lisp/progmodes/python.el (inferior-python-mode): Remove
comint-filename-completion in comint-dynamic-complete-functions.
---
 lisp/progmodes/python.el | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index a26ec9c600b..8fae4164b6a 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -3726,7 +3726,8 @@ inferior-python-mode
   (setq-local compilation-error-regexp-alist
               python-shell-compilation-regexp-alist)
   (setq-local scroll-conservatively 1)
+  (setq-local comint-dynamic-complete-functions
+              '(comint-c-a-p-replace-by-expanded-history))
   (add-hook 'completion-at-point-functions
             #'python-shell-completion-at-point nil 'local)
   (define-key inferior-python-mode-map "\t"
--
2.25.1