From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id sMMlKsUpH2QM2wAASxT56A (envelope-from ) for ; Sat, 25 Mar 2023 18:05:09 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id GOkbKcUpH2RzaQEAG6o9tA (envelope-from ) for ; Sat, 25 Mar 2023 18:05:09 +0100 Received: from lists.gnu.org (unknown [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 5BC67F331 for ; Sat, 25 Mar 2023 18:05:09 +0100 (CET) Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=mailbaby.net header.s=bambino header.b=fVwMqne0; dkim=fail ("headers rsa verify failed") header.d=tatersworld.org header.s=x header.b=qhB1TXbm; arc=pass ("mailbaby.net:s=detka:i=1"); spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=none ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1679763909; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=6IdjBZn9B1O2WUXP+EGZW/ivLUsoP9mBYigHj2kqEqs=; b=GkKXGC0il2CNYTwIuF/qT0uSUbtyZR44YY8SvRQSytyBzy9KkP/5lR8ceyx93L8xIBKpOu JaStE7KBjsiAyPDS9lGz0l7x8JWgcbs4Krg9cVFBnqOhWfUlDDylbFvg25/mBXqkFr0Xw6 tI847/nmvA6e3N8V5EX0uQlYQduHPC1jTTtzwp+yiKYo4a+E/ssOyMnqmHgzBajo+Mumbx HjDUx/TbFgKLXYbc8kHyMb+kybeQzeNqNoYQN+lDOxb5HJEmwEnQ0xZJoPafkmOrNNftHw wo5faF+AqxhlbuOzcXI0Z8fGQIj3b18F0Nzopu9NB1h/SPiJZBGrHK2CrM6T7A== ARC-Seal: i=2; s=key1; d=yhetil.org; t=1679763909; a=rsa-sha256; cv=pass; b=g++PaQqDGWnxSXTxJNwC/JUzXtbYSKmszyXoXnaI3d0A8JYae+a2n3aqYb+alG32dGrO/l 2Dr+ICsEGA8drf5KpJXCMi7w/jpbdR68aFub743UQu9eadImK9NVJ6lhY0qPGgq5rr925C hxyxzLFFy3eY+TGfJXB3uM0kMGtfNpZ1k4aBayKn7LWt03KdDV3V0ptG2oSvHxjD7CsG6x GeHolGsrwJ23kRXXXv75fO1IevGyn927j17vLhsj29Z2xZUGsTDxhhHyun0WMocrxAD0CX AQtAR/JVTa6OWVOwCmsLaL+O3bO0VroBtkvDGis/i+QVtGayUFqjoPXumgNx5Q== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=mailbaby.net header.s=bambino header.b=fVwMqne0; dkim=fail ("headers rsa verify failed") header.d=tatersworld.org header.s=x header.b=qhB1TXbm; arc=pass ("mailbaby.net:s=detka:i=1"); spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org"; dmarc=none Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pg7Ji-0001P0-NN; Sat, 25 Mar 2023 13:04:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pg7Jh-0001Oq-Hj for emacs-orgmode@gnu.org; Sat, 25 Mar 2023 13:04:33 -0400 Received: from relay0-e.mailbaby.net ([199.231.189.158]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pg7Jf-0002Mk-S8 for emacs-orgmode@gnu.org; Sat, 25 Mar 2023 13:04:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbaby.net; q=dns/txt; s=bambino; bh=6IdjBZn9B1O2WUXP+EGZW/ivLUsoP9mBYigHj2kqEqs=; h=from:subject:date:message-id:to:cc:mime-version:content-type:in-reply-to:references; b=fVwMqne0RFw+vVTBv7i/bfoDkk6+5HqDVje/EXngYHjvhaMQTiihYu+Ap5FhJ19MU9KSO7Uv+ Ba7ZNMmgpJW6FG7CFJ+P9xvMGrzv2NUW3HdtGJn0X7fdPkkPQ6X5cBXRz5G/Y0bIR1L6jXk0quu D0KZ4NQpl9vLe+anVKKaZYc= Received: from zmta1 ([45.76.239.126] 45.76.239.126.vultrusercontent.com) (Authenticated sender: mb25440) by relay0-e.mailbaby.net (InterServerMTA) with ESMTPSA id 18719ba7d9200033c6.001 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256); Sat, 25 Mar 2023 17:04:25 +0000 X-Zone-Loop: 50ff2dc84ffb46e077ab2fb3130320d09e6b81bf681f ARC-Authentication-Results: i=1; relay0.mailbaby.net; auth=pass smtp.auth=mb25440 smtp.mailfrom=jackkamm@tatersworld.org ARC-Seal: i=1; s=detka; d=mailbaby.net; t=1679763865; a=rsa-sha256; cv=none; b=yb6DMMRopouUbKP/V37a44xW5HSRUIkXf3zH4KSoLq6qj3uyYHLuMaIv85tdM0GKVXab6g b+iwg3aWzR5TYyKfSMbzcZMjkljIu1156FKw9Wpcs74sR+4/SqnIgjT/DyytV2PA8MHyRO Dnmi4heOLnfI/JMbhj6not5o58Yt217pHeYq6qiWasqy53kIAG40kxl6f6q5zGZPpElDOQ Y3HBA8D1oNMabuTphXZVA/XTbv1trq12++u6lPRt2LKbrlfSutMgq3RtVqtGVJm9FQDR4Q dwpWnWIWsGMG/C0v64bE8jmooWUqFVazjmdUYKvg1QKuuCYz2V1MeoeTQwfTVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbaby.net; s=detka; t=1679763865; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6IdjBZn9B1O2WUXP+EGZW/ivLUsoP9mBYigHj2kqEqs=; b=KgHl+M+pZ+2Q4whklMsYz8Ufl0YWcl1B1gMFQTHZ+yFKi0qmQDQ1931yYyA1UFEu0AeT6I uiZ9tr/I0JZnimss23NdF1Un+RdqE6BT/eBiIznH6wZT/pa9AdWGBfQLeynzp1v/v/AxMk pUcbA6Tshg2R40Mtb1rYoZd2GYpT9C85hPgNsfVSQISTI3J9tbmNwFxV7ze5xTorZaSXon bRQgcCRsoMGV5poySrz0zFPCvM/nDYhaNKkJOJwCnDj2MCGdGY1g4SmPLFq2T25w1Yb13S lMc0OAjyR2CWjaEC3TlmJ7qLC+WsjZb1ez+pXhaVfbPHaBUHrpnOpcF6puRakg== Received: from mail-111-mta2.mxroute.com ([136.175.111.2] filter006.mxroute.com) (Authenticated sender: mN4UYu2MZsgR) by zmta1 (ZoneMTA) with ESMTPSA id 18719b5a8d7000edb4.002 for (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256); Sat, 25 Mar 2023 16:59:08 +0000 X-Zone-Loop: ba45d8eef128e53f7eb198769f0622f0bfff3f40c4b9 X-Originating-IP: [45.76.239.126] DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tatersworld.org; s=x; h=Content-Type:MIME-Version:Message-ID:Date: References:In-Reply-To:Subject:Cc:To:From:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=6IdjBZn9B1O2WUXP+EGZW/ivLUsoP9mBYigHj2kqEqs=; b=qhB1TXbmzDBgZzv5oBBICoRSuA kzus8bllX9sJs8t9oeocf2/7Ql0rxJgyjXHa9zNQLx9+8hik4I3pWgA59iy1V8/Jhfmbugf7+yTSw GIzp2GWqqEqz4x3ZdiSyw5L9CAHHHwPWsR1GbDWwBpI6AZEvHt0jTCoaLdInOqk/L8CkMZ6t+h6wp r+2GD2lC6kyiIE3lVaJlWSaPCZNqrQfJcT9m/OLpDmjDzoqZQO/uogi+Qgh0uWOQyOsclvHVzQySF ZlqY/TskI8EXwTjNfdtaETSeRG2dsOaZL6llTyXSCjSj1bd2WeNojCmXvmnAbUICZUdqL6Vu5HLbB eYatlUyg==; From: Jack Kamm To: Ihor Radchenko Cc: Matt , emacs-orgmode Subject: [PATCH] Fix externally started sessions with ob-python In-Reply-To: <87a6006aa9.fsf@gmail.com> References: <186283d230a.129f5feb61660123.3289004102603503414@excalamus.com> <87a61j3orj.fsf@gmail.com> <1864701b4b8.e37160d6373275.4308190093579612690@excalamus.com> <87357a2r27.fsf@gmail.com> <1864bbf632d.acf83486624816.5548943856411522610@excalamus.com> <87wn4j1mee.fsf@gmail.com> <875yc1wz37.fsf@localhost> <87ttzhy9vm.fsf@gmail.com> <87a6006aa9.fsf@gmail.com> Date: Sat, 25 Mar 2023 09:59:05 -0700 Message-ID: <877cv46a3q.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Authenticated-Id: jackkamm@tatersworld.org Received-SPF: pass client-ip=199.231.189.158; envelope-from=jackkamm@tatersworld.org; helo=relay0-e.mailbaby.net X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: X-Migadu-Queue-Id: 5BC67F331 X-Spam-Score: -6.69 X-Migadu-Spam-Score: -6.69 X-Migadu-Scanner: scn0.migadu.com List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN X-TUID: hM3ptcviyFdG --=-=-= Content-Type: text/plain Jack Kamm writes: > Jack Kamm 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. Ugh, sorry, I meant to start a thread with a new Subject for this. Re-sending the patch here, with the updated email Subject this time. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-ob-python-Allow-working-with-externally-started-sess.patch >From 47bf309b9af4cd342d3e939442504a9d34bf6592 Mon Sep 17 00:00:00 2001 From: Jack Kamm 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 --=-=-=--