Hi,

To reproduce the bug:

1. create test.org:
──────────────────✀──────────────────
#+begin_src python :session "*Python 3*" :async t
1
#+end_src

# Local Variables:
# python-shell-buffer-name: "Python 3"
# End:
──────────────────✀──────────────────

2. emacs -Q -L <path_to_latest_org> --eval "(require 'ob-python)"

3. Open test.org, then start a python shell with M-x run-python, which
should create a buffer named "*Python 3*"

4. Press C-c C-c on the src block. Then an error "No inferior Python
process running" is shown.
From 75ca16a21fe409aeb37b9bf0d97895c00f38d466 Mon Sep 17 00:00:00 2001
From: Liu Hui <liuhui1...@gmail.com>
Date: Tue, 11 Jul 2023 10:49:07 +0800
Subject: [PATCH] ob-python: Fix async evaluation

* lisp/ob-python.el (org-babel-python-async-evaluate-session): Bind
`python-shell-buffer-name' inside the temp buffer.
---
 lisp/ob-python.el | 47 +++++++++++++++++++++++++----------------------
 1 file changed, 25 insertions(+), 22 deletions(-)

diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 0e0539d7a..c15d45b96 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -400,28 +400,31 @@ (defun org-babel-python-async-evaluate-session
    session (current-buffer)
    "ob_comint_async_python_\\(.+\\)_\\(.+\\)"
    'org-babel-chomp 'org-babel-python-async-value-callback)
-  (let ((python-shell-buffer-name (org-babel-python-without-earmuffs session)))
-    (pcase result-type
-      (`output
-       (let ((uuid (org-id-uuid)))
-         (with-temp-buffer
-           (insert (format org-babel-python-async-indicator "start" uuid))
-           (insert "\n")
-           (insert body)
-           (insert "\n")
-           (insert (format org-babel-python-async-indicator "end" uuid))
-           (python-shell-send-buffer))
-         uuid))
-      (`value
-       (let ((tmp-results-file (org-babel-temp-file "python-"))
-             (tmp-src-file (org-babel-temp-file "python-")))
-         (with-temp-file tmp-src-file (insert body))
-         (with-temp-buffer
-           (insert (org-babel-python-format-session-value tmp-src-file tmp-results-file result-params))
-           (insert "\n")
-           (insert (format org-babel-python-async-indicator "file" tmp-results-file))
-           (python-shell-send-buffer))
-         tmp-results-file)))))
+  (pcase result-type
+    (`output
+     (let ((uuid (org-id-uuid)))
+       (with-temp-buffer
+         (insert (format org-babel-python-async-indicator "start" uuid))
+         (insert "\n")
+         (insert body)
+         (insert "\n")
+         (insert (format org-babel-python-async-indicator "end" uuid))
+         (let ((python-shell-buffer-name
+                (org-babel-python-without-earmuffs session)))
+           (python-shell-send-buffer)))
+       uuid))
+    (`value
+     (let ((tmp-results-file (org-babel-temp-file "python-"))
+           (tmp-src-file (org-babel-temp-file "python-")))
+       (with-temp-file tmp-src-file (insert body))
+       (with-temp-buffer
+         (insert (org-babel-python-format-session-value tmp-src-file tmp-results-file result-params))
+         (insert "\n")
+         (insert (format org-babel-python-async-indicator "file" tmp-results-file))
+         (let ((python-shell-buffer-name
+                (org-babel-python-without-earmuffs session)))
+           (python-shell-send-buffer)))
+       tmp-results-file))))
 
 (provide 'ob-python)
 
-- 
2.25.1

Reply via email to