From d5721aa37a399afcd527906e5d9f1b6bce37fdb9 Mon Sep 17 00:00:00 2001 From: Jack Kamm Date: Sun, 27 Aug 2023 14:13:15 -0700 Subject: [PATCH] ob-python: Fix hanging on second start See: https://list.orgmode.org/87ttsnh5bx.fsf@localhost/T/#t * lisp/ob-python.el (org-babel-python-initiate-session-by-key): Switch from `org-babel-comint-wait-for-output' to `sleep-for' while waiting for `org-babel-python--initialized', to prevent hanging on restarted Python process. * testing/lisp/test-ob-python.el (test-ob-python/session-restart): New test for restarting ob-python session process. --- lisp/ob-python.el | 2 +- testing/lisp/test-ob-python.el | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lisp/ob-python.el b/lisp/ob-python.el index b9dab91b4..6b216ce89 100644 --- a/lisp/ob-python.el +++ b/lisp/ob-python.el @@ -272,7 +272,7 @@ (defun org-babel-python-initiate-session-by-key (&optional session) ;; multiple prompts during initialization. (with-current-buffer py-buffer (while (not org-babel-python--initialized) - (org-babel-comint-wait-for-output py-buffer))) + (sleep-for 0 10))) (setq org-babel-python-buffers (cons (cons session py-buffer) (assq-delete-all session org-babel-python-buffers))) diff --git a/testing/lisp/test-ob-python.el b/testing/lisp/test-ob-python.el index 82fbca36e..c11e1d0c2 100644 --- a/testing/lisp/test-ob-python.el +++ b/testing/lisp/test-ob-python.el @@ -310,6 +310,25 @@ (ert-deftest test-ob-python/async-local-python-shell () #+end_src" (should (org-babel-execute-src-block)))) +(ert-deftest test-ob-python/session-restart () + ;; Disable the test on older Emacs as built-in python.el sometimes + ;; fail to initialize session. + (skip-unless (version<= "28" emacs-version)) + (should + (equal "success" + (progn + (org-test-with-temp-text "#+begin_src python :session :results output +print('start') +#+end_src" + (org-babel-execute-src-block)) + (let ((proc (python-shell-get-process))) + (python-shell-send-string "exit()") + (while (accept-process-output proc))) + (org-test-with-temp-text "#+begin_src python :session :results output +print('success') +#+end_src" + (org-babel-execute-src-block)))))) + (provide 'test-ob-python) ;;; test-ob-python.el ends here -- 2.41.0