From 9a6ca1c11a2849753fd3b854a79561224629a6bf Mon Sep 17 00:00:00 2001 From: Evgenii Klimov Date: Tue, 27 Aug 2024 23:08:47 +0100 Subject: [PATCH] Keep project's exec-path during with-temp-buffer call * lisp/progmodes/python.el (python-shell-prompt-detect): `with-temp-buffer' doesn't respect buffer-local environment variables, `exec-path' in this case. Which results in executables not being found, or the wrong versions of executables being picked up. E.g. if env var is modified via .dir-local file or direnv/envrc package. --- lisp/progmodes/python.el | 44 +++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 7193cc19425..d6bb409c286 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -3116,27 +3116,29 @@ (defun python-shell-prompt-detect () (interpreter python-shell-interpreter) (interpreter-arg python-shell-interpreter-interactive-arg) (output - (with-temp-buffer - ;; TODO: improve error handling by using - ;; `condition-case' and displaying the error message to - ;; the user in the no-prompts warning. - (ignore-errors - (let ((code-file - ;; Python 2.x on Windows does not handle - ;; carriage returns in unbuffered mode. - (let ((inhibit-eol-conversion (getenv "PYTHONUNBUFFERED"))) - (python-shell--save-temp-file code)))) - (unwind-protect - ;; Use `process-file' as it is remote-host friendly. - (process-file - interpreter - code-file - '(t nil) - nil - interpreter-arg) - ;; Try to cleanup - (delete-file code-file)))) - (buffer-string))) + (let ((current-exec-path exec-path)) + (with-temp-buffer + ;; TODO: improve error handling by using + ;; `condition-case' and displaying the error message to + ;; the user in the no-prompts warning. + (ignore-errors + (let ((code-file + ;; Python 2.x on Windows does not handle + ;; carriage returns in unbuffered mode. + (let ((inhibit-eol-conversion (getenv "PYTHONUNBUFFERED"))) + (python-shell--save-temp-file code)))) + (unwind-protect + ;; Use `process-file' as it is remote-host friendly. + (let ((exec-path current-exec-path)) + (process-file + interpreter + code-file + '(t nil) + nil + interpreter-arg)) + ;; Try to cleanup + (delete-file code-file)))) + (buffer-string)))) (prompts (catch 'prompts (dolist (line (split-string output "\n" t)) -- 2.45.2