diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 5a820f05d77..848a26229e6 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -3816,6 +3816,16 @@ variable. (compilation-shell-minor-mode 1) (python-pdbtrack-setup-tracking)) +(defvar-local python-shell--process-cache) +(defvar-local python-shell--process-cache-valid) + +(defun python-shell--invalidate-process-cache () + "Invalidate process cache." + (dolist (buffer (buffer-list)) + (with-current-buffer buffer + (setq python-shell--process-cache nil + python-shell--process-cache-valid nil)))) + (defun python-shell-make-comint (cmd proc-name &optional show internal) "Create a Python shell comint buffer. CMD is the Python command to be executed and PROC-NAME is the @@ -3832,6 +3842,7 @@ killed." (let* ((proc-buffer-name (format (if (not internal) "*%s*" " *%s*") proc-name))) (when (not (comint-check-proc proc-buffer-name)) + (python-shell--invalidate-process-cache) (let* ((cmdlist (split-string-and-unquote cmd)) (interpreter (car cmdlist)) (args (cdr cmdlist)) @@ -3955,7 +3966,15 @@ If current buffer is in `inferior-python-mode', return it." (defun python-shell-get-process () "Return inferior Python process for current buffer." - (get-buffer-process (python-shell-get-buffer))) + (unless (and python-shell--process-cache-valid + (or (not python-shell--process-cache) + (and (process-live-p python-shell--process-cache) + (buffer-live-p + (process-buffer python-shell--process-cache))))) + (setq python-shell--process-cache + (get-buffer-process (python-shell-get-buffer)) + python-shell--process-cache-valid t)) + python-shell--process-cache) (defun python-shell-get-process-or-error (&optional interactivep) "Return inferior Python process for current buffer or signal error.