From: Jack Kamm <jackkamm@tatersworld.org>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: Matt <matt@excalamus.com>, emacs-orgmode <emacs-orgmode@gnu.org>
Subject: Re: [BUG] shell sessions started outside of Babel broken
Date: Sat, 25 Mar 2023 09:55:10 -0700 [thread overview]
Message-ID: <87a6006aa9.fsf@gmail.com> (raw)
In-Reply-To: <87ttzhy9vm.fsf@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 473 bytes --]
Jack Kamm <jackkamm@tatersworld.org> writes:
> I think it's worthwhile for Org Babel to support external sessions
> where feasible. ob-R and ob-python have long supported this, though
> recent changes to ob-python seem to have broken it there (I'm looking
> into fixing that).
I'm attaching a patch that restores ob-python's ability to work with
externally started sessions.
I'll plan to push this commit in a few days, unless any problems with
the patch are noticed.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-python-Allow-working-with-externally-started-sess.patch --]
[-- Type: text/x-patch, Size: 3839 bytes --]
From 47bf309b9af4cd342d3e939442504a9d34bf6592 Mon Sep 17 00:00:00 2001
From: Jack Kamm <jackkamm@gmail.com>
Date: Sat, 25 Mar 2023 07:53:28 -0700
Subject: [PATCH] ob-python: Allow working with externally started sessions
again
* lisp/ob-python.el (python-shell-buffer-name): Remove unneeded
defvar.
(org-babel-python-initiate-session-by-key): Check if session already
existed before run-python. Only wait for initialization if it's a
newly started session. Also simplify the code a bit by combining
multiple setq and let statements into a single let statement. Also
add a comment about why adding to `python-shell-first-prompt-hook'
after `run-python' should be safe from race conditions.
---
lisp/ob-python.el | 43 ++++++++++++++++++++++++-------------------
1 file changed, 24 insertions(+), 19 deletions(-)
diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 456f2d489..0e0539d7a 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -169,7 +169,6 @@ (defun org-babel-python-without-earmuffs (session)
(substring name 1 (- (length name) 1))
name)))
-(defvar python-shell-buffer-name)
(defvar-local org-babel-python--initialized nil
"Flag used to mark that python session has been initialized.")
(defun org-babel-python-initiate-session-by-key (&optional session)
@@ -178,27 +177,33 @@ (defun org-babel-python-initiate-session-by-key (&optional session)
then create. Return the initialized session."
(save-window-excursion
(let* ((session (if session (intern session) :default))
- (py-buffer (org-babel-python-session-buffer session))
+ (py-buffer (or (org-babel-python-session-buffer session)
+ (org-babel-python-with-earmuffs session)))
(cmd (if (member system-type '(cygwin windows-nt ms-dos))
(concat org-babel-python-command " -i")
- org-babel-python-command)))
- (unless py-buffer
- (setq py-buffer (org-babel-python-with-earmuffs session)))
- (let ((python-shell-buffer-name
- (org-babel-python-without-earmuffs py-buffer)))
- (run-python cmd)
- (with-current-buffer py-buffer
- (add-hook
- 'python-shell-first-prompt-hook
- (lambda () (setq-local org-babel-python--initialized t))
- nil 'local)))
- ;; Wait until Python initializes.
- ;; This is more reliable compared to
- ;; `org-babel-comint-wait-for-output' as python may emit
- ;; multiple prompts during initialization.
+ org-babel-python-command))
+ (python-shell-buffer-name
+ (org-babel-python-without-earmuffs py-buffer))
+ (existing-session-p (comint-check-proc py-buffer)))
+ (run-python cmd)
(with-current-buffer py-buffer
- (while (not org-babel-python--initialized)
- (org-babel-comint-wait-for-output py-buffer)))
+ ;; Adding to `python-shell-first-prompt-hook' immediately
+ ;; after `run-python' should be safe from race conditions,
+ ;; because subprocess output only arrives when Emacs is
+ ;; waiting (see elisp manual, "Output from Processes")
+ (add-hook
+ 'python-shell-first-prompt-hook
+ (lambda () (setq-local org-babel-python--initialized t))
+ nil 'local))
+ ;; Don't hang if session was started externally
+ (unless existing-session-p
+ ;; Wait until Python initializes
+ ;; This is more reliable compared to
+ ;; `org-babel-comint-wait-for-output' as python may emit
+ ;; multiple prompts during initialization.
+ (with-current-buffer py-buffer
+ (while (not org-babel-python--initialized)
+ (org-babel-comint-wait-for-output py-buffer))))
(setq org-babel-python-buffers
(cons (cons session py-buffer)
(assq-delete-all session org-babel-python-buffers)))
--
2.39.2
next prev parent reply other threads:[~2023-03-25 17:02 UTC|newest]
Thread overview: 72+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-06 19:39 [PATCH] Async evaluation in ob-shell Matt
2023-02-07 11:40 ` Ihor Radchenko
2023-02-09 4:33 ` Matt
2023-02-09 11:24 ` Ihor Radchenko
2023-02-10 22:19 ` Matt
2023-02-11 11:44 ` Ihor Radchenko
2023-02-12 19:32 ` Matt
2023-02-15 15:08 ` Ihor Radchenko
2023-02-16 4:02 ` Matt
2023-02-17 10:44 ` Ihor Radchenko
2023-02-19 23:14 ` Matt
2023-02-20 11:24 ` Ihor Radchenko
2023-02-20 17:24 ` Matt
2023-02-22 10:30 ` Ihor Radchenko
2023-03-02 1:36 ` Matt
2023-03-03 14:52 ` Ihor Radchenko
2023-03-03 17:53 ` Matt
2023-03-05 12:15 ` Ihor Radchenko
2023-03-06 6:45 ` Matt
2023-03-07 12:45 ` Ihor Radchenko
2023-03-09 17:36 ` Matt
2023-03-10 1:52 ` Max Nikulin
2023-03-12 16:28 ` Jack Kamm
2023-03-18 10:48 ` Ihor Radchenko
2023-03-21 20:29 ` Matt
2023-03-22 12:12 ` Ihor Radchenko
2023-03-23 11:50 ` Ihor Radchenko
2023-03-23 19:35 ` Matt
2023-03-24 9:13 ` Ihor Radchenko
2023-03-28 2:53 ` Matt
2023-03-28 10:06 ` Ihor Radchenko
2023-04-17 15:31 ` Matt
2023-04-17 18:55 ` Ihor Radchenko
2023-04-17 18:56 ` Matt
2023-04-17 19:05 ` Ihor Radchenko
2023-03-23 3:25 ` [SUGGESTION] ob-shell async result output should not contains shell prompt Christopher M. Miles
2023-03-23 4:21 ` Matt
2023-03-23 11:12 ` Christopher M. Miles
2023-03-23 16:23 ` Matt
2023-03-24 11:20 ` Ihor Radchenko
2023-03-23 16:26 ` Remove "shell" as a supported Babel language within ob-shell.el (was Re: [SUGGESTION] ob-shell async result output should not contains shell prompt) Matt
2023-03-24 1:53 ` Remove "shell" as a supported Babel language within ob-shell.el Christopher M. Miles
2023-03-24 11:38 ` Remove "shell" as a supported Babel language within ob-shell.el (was Re: [SUGGESTION] ob-shell async result output should not contains shell prompt) Ihor Radchenko
2023-03-25 5:47 ` Samuel Wales
2023-03-25 18:07 ` Ihor Radchenko
2023-03-28 2:33 ` Matt
2023-02-11 20:56 ` [PATCH] Async evaluation in ob-shell jackkamm
2023-02-12 19:02 ` Matt
2023-02-13 3:16 ` Jack Kamm
2023-02-13 17:07 ` [BUG] shell sessions started outside of Babel broken Matt
2023-02-15 6:19 ` Jack Kamm
2023-02-16 12:53 ` Ihor Radchenko
2023-02-19 15:04 ` Jack Kamm
2023-02-20 11:22 ` Ihor Radchenko
2023-02-21 5:23 ` Jack Kamm
2023-02-22 10:38 ` Ihor Radchenko
2023-03-25 16:55 ` Jack Kamm [this message]
2023-03-25 16:59 ` [PATCH] Fix externally started sessions with ob-python Jack Kamm
2023-02-13 20:11 ` [BUG] conda doesn't work in ob-shell sessions Matt
2023-02-15 6:21 ` Jack Kamm
2024-01-18 11:55 ` Ihor Radchenko
2024-01-21 22:48 ` Jack Kamm
2024-01-22 3:42 ` Jack Kamm
2024-01-22 11:59 ` Ihor Radchenko
2024-01-23 6:09 ` Jack Kamm
2024-01-24 15:22 ` Ihor Radchenko
2024-01-25 19:14 ` Matt
2024-01-25 20:36 ` Ihor Radchenko
2024-01-26 0:42 ` Jack Kamm
2024-01-27 10:25 ` Matt
2024-02-09 16:37 ` Ihor Radchenko
2024-01-23 18:51 ` Suhail Singh
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
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87a6006aa9.fsf@gmail.com \
--to=jackkamm@tatersworld.org \
--cc=emacs-orgmode@gnu.org \
--cc=matt@excalamus.com \
--cc=yantar92@posteo.net \
/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 public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).