all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: kobarity <kobarity@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>, Evgenii Klimov <eugene.dev@lipklim.org>
Cc: 72849@debbugs.gnu.org
Subject: bug#72849: [PATCH] Keep project's exec-path during with-temp-buffer call
Date: Fri, 30 Aug 2024 01:08:25 +0900	[thread overview]
Message-ID: <eke78qwfz4zq.wl-kobarity@gmail.com> (raw)
In-Reply-To: <86y14ghmm1.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1447 bytes --]

Eli Zaretskii wrote:
> > Date: Wed, 28 Aug 2024 00:13:25 +0100
> > From:  Evgenii Klimov via "Bug reports for GNU Emacs,
> >  the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
> > 
> > `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
> > environment variable is modified via .dir-local file or direnv/envrc
> > package.
> 
> Hmm, this doesn't look clean to me: exec-path is just one variable,
> what makes it special here?
> 
> Moreover, it sounds like python-shell-with-environment, which
> python-shell-prompt-detect calls, already attempts to have
> buffer-local value of exec-path to be available to Python, so why
> isn't that working for you?  And if it isn't work, I think we should
> amend python-shell-with-environment to do this, so we don't need to do
> it "by hand".
> 
> kobarity, any comments or suggestions?

The problem is that `with-temp-buffer' creates a new buffer, so the
buffer local value of `exec-path' is not maintained in the new buffer.
It is used to find the Python interpreter unless the interpreter is
specified using the absolute path.

I think Evgenii's patch is logically correct, but it would be better
to create a modified version of `with-temp-buffer' which keeps
`exec-path' in the new buffer.  The attached is my proposal to do so.

[-- Attachment #2: 0001-Fix-Python-prompt-detection-when-exec-path-is-buffer.patch --]
[-- Type: application/octet-stream, Size: 1906 bytes --]

From b94ba0d4e07635a0abddf0cba7548b10b47dad92 Mon Sep 17 00:00:00 2001
From: kobarity <kobarity@gmail.com>
Date: Fri, 30 Aug 2024 00:13:27 +0900
Subject: [PATCH] Fix Python prompt detection when 'exec-path' is buffer local

* lisp/progmodes/python.el (python-with-temp-buffer): New macro
same as 'with-temp-buffer', except the variable 'exec-path' is
kept in the new buffer.
(python-shell-prompt-detect): Use it. (Bug#72849)

Co-authored-by: Evgenii Klimov <eugene.dev@lipklim.org>
---
 lisp/progmodes/python.el | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index a00289d6de9..480713c3aca 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -3088,6 +3088,16 @@ 'python--parse-json-array
         (json-read-from-string string))))
   "Parse the JSON array in STRING into a Lisp list.")
 
+(defmacro python-with-temp-buffer (&rest body)
+  "Python mode specific version of `with-temp-buffer'.
+Call `with-temp-buffer' with BODY.  The variable `exec-path' is kept in
+the new buffer even if it is buffer local."
+  (declare (indent 0) (debug t))
+  `(let ((python--exec-path exec-path))
+     (with-temp-buffer
+       (let ((exec-path python--exec-path))
+         ,@body))))
+
 (defun python-shell-prompt-detect ()
   "Detect prompts for the current `python-shell-interpreter'.
 When prompts can be retrieved successfully from the
@@ -3116,7 +3126,7 @@ python-shell-prompt-detect
              (interpreter python-shell-interpreter)
              (interpreter-arg python-shell-interpreter-interactive-arg)
              (output
-              (with-temp-buffer
+              (python-with-temp-buffer
                 ;; TODO: improve error handling by using
                 ;; `condition-case' and displaying the error message to
                 ;; the user in the no-prompts warning.
-- 
2.34.1


  reply	other threads:[~2024-08-29 16:08 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-27 23:13 bug#72849: [PATCH] Keep project's exec-path during with-temp-buffer call Evgenii Klimov via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-28  7:12 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-28 12:12 ` Eli Zaretskii
2024-08-29 16:08   ` kobarity [this message]
2024-08-29 16:25     ` Eli Zaretskii
2024-08-29 22:51   ` Evgenii Klimov via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-08-30 14:43     ` kobarity
2024-08-31 10:17       ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eke78qwfz4zq.wl-kobarity@gmail.com \
    --to=kobarity@gmail.com \
    --cc=72849@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=eugene.dev@lipklim.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.