From 56fd5e05bc33337dc82fb825416100e75663a520 Mon Sep 17 00:00:00 2001 From: Liu Hui Date: Wed, 12 Jul 2023 18:07:06 +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. * testing/lisp/test-ob-python.el (test-ob-python/async-local-python-shell): Add test. --- lisp/ob-python.el | 47 ++++++++++++++++++---------------- testing/lisp/test-ob-python.el | 15 +++++++++++ 2 files changed, 40 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) diff --git a/testing/lisp/test-ob-python.el b/testing/lisp/test-ob-python.el index 7aac87116..14dae0ef5 100644 --- a/testing/lisp/test-ob-python.el +++ b/testing/lisp/test-ob-python.el @@ -296,6 +296,21 @@ (ert-deftest test-ob-python/async-output-drawer () (string= (concat src-block result) (buffer-string))))))) +(ert-deftest test-ob-python/async-local-python-shell () + ;; Disable the test on older Emacs as built-in python.el sometimes + ;; fail to initialize session. + (skip-unless (version<= "28" emacs-version)) + (when-let ((buf (get-buffer "*Python*"))) + (let (kill-buffer-query-functions) + (kill-buffer buf))) + (org-test-with-temp-text-in-file + "# -*- python-shell-buffer-name: \"Python 3\" -*- +#+begin_src python :session \"*Python 3*\" :async yes +1 +#+end_src" + (run-python nil nil 'hide) + (should (org-babel-execute-src-block)))) + (provide 'test-ob-python) ;;; test-ob-python.el ends here -- 2.25.1