Subject: bug#79036: [PATCH] Fix pdb tracking for remote filenames

From: Liu Hui <[email-masked]>
Date: Fri, 18 Jul 2025 12:47:07 +0800

On Thu, Jul 17, 2025 at 10:31 PM Eli Zaretskii <[email-masked]> wrote:

> So we need a function called, say, remote-file-name, which, when given
> a file name, even an absolute one, will consider it to be on the
> remote host indicated by default-directory, and will be equivalent to
> expand-file-name if default-directory is a local one, is that right?
>
> Michael, don't we already have such a function?
>
> If we did have such a function, wouldn't it be enough to just call
>
>    (find-file-noselect (remote-file-name file-name))
>
> ?  That is, we won't need to call get-file-buffer, nor file-exists-p,
> right?  Or am I still missing something?

They are still needed because python-pdbtrack-set-tracked-buffer is
not just used to visit file-name.

If the file-buffer already exists, which uses get-file-buffer for
checking, we don't kill it when pdb finishes. Otherwise, the
file-buffer will be created by find-file-noselect and added to
python-pdbtrack-buffers-to-kill.

file-exists-p: For a local file name in a remote python shell, we
cannot simply distinguish whether it is a file on the remote system or
a local file (i.e. sending code from local host), although the former
is more likely. So we need file-exists-p for verification.

Moreover, if file doesn't exist (e.g. the original file may be
deleted), it makes no sense to create an empty buffer/file, because we
don't want to edit it, but to debug the code in it.

The attached patch fixes the original bug and another one of pdbtrack
(described earlier). It also adds a check in case
python-pdbtrack-set-tracked-buffer cannot find file.
From 167e1a0808ff9ef1fc5ab523da3812267c8da58a Mon Sep 17 00:00:00 2001
From: Liu Hui <[email-masked]>
Date: Thu, 17 Jul 2025 12:43:34 +0800
Subject: [PATCH] Fix pdb tracking (bug#79036)

* lisp/progmodes/python.el (python-pdbtrack-set-tracked-buffer):
Use correct file names.
(python-pdbtrack-comint-output-filter-function): Do not continue
if `python-pdbtrack-set-tracked-buffer' returns nil.
---
 lisp/progmodes/python.el | 42 ++++++++++++++++++++++++----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index f4f0518dbfd..9b0b4c0ec52 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -5079,9 +5079,14 @@ python-pdbtrack-set-tracked-buffer
   "Set the buffer for FILE-NAME as the tracked buffer.
 Internally it uses the `python-pdbtrack-tracked-buffer' variable.
 Returns the tracked buffer."
-  (let* ((file-name-prospect (concat (file-remote-p default-directory)
-                              file-name))
-         (file-buffer (get-file-buffer file-name-prospect)))
+  (let* ((file-name-prospect (if (file-remote-p file-name)
+                                 file-name
+                               (concat (file-remote-p default-directory)
+                                       file-name)))
+         (file-buffer (or (get-file-buffer file-name-prospect)
+                          ;; file-name is local & Python shell is remote
+                          (and (not (equal file-name file-name-prospect))
+                               (get-file-buffer file-name)))))
     (unless file-buffer
       (cond
        ((file-exists-p file-name-prospect)
@@ -5089,8 +5094,9 @@ python-pdbtrack-set-tracked-buffer
        ((and (not (equal file-name file-name-prospect))
              (file-exists-p file-name))
         ;; Fallback to a locally available copy of the file.
-        (setq file-buffer (find-file-noselect file-name-prospect))))
-      (when (and python-pdbtrack-kill-buffers
+        (setq file-buffer (find-file-noselect file-name))))
+      (when (and file-buffer
+                 python-pdbtrack-kill-buffers
                  (not (member file-buffer python-pdbtrack-buffers-to-kill)))
         (add-to-list 'python-pdbtrack-buffers-to-kill file-buffer)))
     (setq python-pdbtrack-tracked-buffer file-buffer)
@@ -5178,18 +5184,20 @@ python-pdbtrack-comint-output-filter-function
                  (shell-buffer (current-buffer))
                  (tracked-buffer-window (get-buffer-window tracked-buffer))
                  (tracked-buffer-line-pos))
-            (with-current-buffer tracked-buffer
-              (setq-local overlay-arrow-position (make-marker))
-              (setq tracked-buffer-line-pos (progn
-                                              (goto-char (point-min))
-                                              (forward-line (1- line-number))
-                                              (point-marker)))
-              (when tracked-buffer-window
-                (set-window-point
-                 tracked-buffer-window tracked-buffer-line-pos))
-              (set-marker overlay-arrow-position tracked-buffer-line-pos))
-            (pop-to-buffer tracked-buffer)
-            (switch-to-buffer-other-window shell-buffer))))))
+            (if (not tracked-buffer)
+                (message "pdbtrack cannot find %s" file-name)
+              (with-current-buffer tracked-buffer
+                (setq-local overlay-arrow-position (make-marker))
+                (setq tracked-buffer-line-pos (progn
+                                                (goto-char (point-min))
+                                                (forward-line (1- line-number))
+                                                (point-marker)))
+                (when tracked-buffer-window
+                  (set-window-point
+                   tracked-buffer-window tracked-buffer-line-pos))
+                (set-marker overlay-arrow-position tracked-buffer-line-pos))
+              (pop-to-buffer tracked-buffer)
+              (switch-to-buffer-other-window shell-buffer)))))))
   output)
 
 (defun python-pdbtrack-setup-tracking ()
-- 
2.25.1