From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id 4E76DSUpH2SOkgAASxT56A (envelope-from ) for ; Sat, 25 Mar 2023 18:02:29 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id QC/TDSUpH2R9PgEAauVa8A (envelope-from ) for ; Sat, 25 Mar 2023 18:02:29 +0100 Received: from lists.gnu.org (lists.gnu.org [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 A9DFACCAB for ; Sat, 25 Mar 2023 18:02:28 +0100 (CET) Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=mailbaby.net header.s=bambino header.b=N1ADJpCr; dkim=fail ("headers rsa verify failed") header.d=tatersworld.org header.s=x header.b=skhCbjGg; 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=1679763748; 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=IWVIi6RL2FVQSpcOof4rzfziLHvppL4V8KrjsHx1M1c=; b=aN1aoFiazSXsl+mkGzScnpppDHUGaH0xU9iS5MoTCfxaWkWa1F4dZ31tuOaF/527UN6+uA KD7lXaz0R9EbaVj4Av8QJbPVEUXFsZg2YXbr/HW1PAqzPdf/xDdn24zAaELZjjLrVzjWET cpbGSNYcJHw+F+hyUrdRiQuV/GSTLibJyiVUFpun1gw0r0IKlwXaRqfWfzNxV/KuYg0TR7 lZPa2sVUOhzotNeVSbwsH4w1f4Zmuf/b+dNeqNqrNU7+4q/UVPxDMzcIVqFODbHuM1ic4E +4EyI8y74tX4PkgPbAdDx40sNtkyVR45Q/I/oLSCc1ereXInfD3qY12g86nrQg== ARC-Seal: i=2; s=key1; d=yhetil.org; t=1679763748; a=rsa-sha256; cv=pass; b=W1uGfAnNxLvv5DXp47tNsxUF69RwlGV24YyRAXirEN2Zkdh4Tu9ztTYmdEHcpRIVWSWuvb q65Fs2mincViPtUxts40zt54gfkjMJYjcPrLKmJl0JcTxou4FD60O0wUgdAoYZqXLm0m6s 7lsXFm7tw0fUl9anz/jpJeQjWW+dw6iX2rdV8XEN8DP9yhrllsaoV14p+TgjUXlkyI5w5X HWEXvfmzyP6OvRM9pQ8AwjRRpu1GQSZPhPcKUD9EbDd3UrkEyRvW6damP85xHBK478uTE7 tPnrKwCMo55HziIy/GDhGOuUBHPe9C36MIJh66EgSQazl5djAF1ATiDvk/gSqA== ARC-Authentication-Results: i=2; aspmx1.migadu.com; dkim=pass header.d=mailbaby.net header.s=bambino header.b=N1ADJpCr; dkim=fail ("headers rsa verify failed") header.d=tatersworld.org header.s=x header.b=skhCbjGg; 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 1pg7Gn-0000GM-NQ; Sat, 25 Mar 2023 13:01:33 -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 1pg7Gl-0000Fr-Hs for emacs-orgmode@gnu.org; Sat, 25 Mar 2023 13:01:31 -0400 Received: from relay2-p.mailbaby.net ([68.168.211.171]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pg7Gi-0001Bp-NY for emacs-orgmode@gnu.org; Sat, 25 Mar 2023 13:01:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbaby.net; q=dns/txt; s=bambino; bh=IWVIi6RL2FVQSpcOof4rzfziLHvppL4V8KrjsHx1M1c=; h=from:subject:date:message-id:to:cc:mime-version:content-type:in-reply-to:references; b=N1ADJpCr7U6hoEfeKxD+QquNrkGclNSDPddTbUmbwNjLlySfHfiwJmfB6biuKF8sNX1lxiaKp oNqWMCq9uHpyTaXobN1xRPxrC1hQVZeCVWoIgtcOs58A8YdQGWPZTdG/x7MvDw+KDkBhW9rJ7lu VW+eDV42ccVDKS6hf6Dm00U= Received: from zmta1 ([45.76.239.126] 45.76.239.126.vultrusercontent.com) (Authenticated sender: mb25440) by relay2-p.mailbaby.net (InterServerMTA) with ESMTPSA id 18719b70c27000e4c0.001 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256); Sat, 25 Mar 2023 17:00:39 +0000 X-Zone-Loop: 5d63c6b9a610cc3bfc3858850e21aabaac1e3cb91729 ARC-Authentication-Results: i=1; rspamd2.mailbaby.net; auth=pass smtp.auth=mb25440 smtp.mailfrom=jackkamm@tatersworld.org ARC-Seal: i=1; s=detka; d=mailbaby.net; t=1679763639; a=rsa-sha256; cv=none; b=kB2lnN3GrsBXgVNC/d833V5ov6DMsJ94i4TUBZdqHxNtqKhQSsb7m2pjJmztPO3udIn46V U7S/OOvE+IzoMASScFXz66BpykkouOsQtceVvNL65RqwBvr3dL5SJt+YpI/TE3wH0j9sCI tLSlZIMzCiiT6hEjkrMZt6CDdrWtwrzUJa7StXYDxu+FNLkaX989XjSkqT80tLDTxHEMjf DBMpYtoM7TdURCLkPRhmxUdlIKczl6KOHVbm/pJYjJfUcLIPL/jD/YF1aqGe2mF2e++4vC 7JxsTDIzZhhTs2cC3/IdhIx8zaiuaHLPKhQIMPF4Y3U2cHfyErhJozpO+7tKHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbaby.net; s=detka; t=1679763639; 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=IWVIi6RL2FVQSpcOof4rzfziLHvppL4V8KrjsHx1M1c=; b=Y3N4TB20IjYi06Ofp9CeeVsB5V17T8GzZquwLpo01vSK/3oU/NLHjCYkUejZXLAYdm/QXn /m+3r3JxcIQiSm0YOoWKpmiHXO4Z1soi0uTTqPgb2AL1qtrjcJ/Q5J+R8gvkagY/88tNh8 K++347E3lmsSIBWimUuU967+uBrSXHYGp0xGZa/Z9NxUDCDOPxe07MiP26cPPB/jtw5Xou SUHbTE9WVWwoIhJSqiWzkNJsHTg4ZVvwu2ZJ6q0ub6HKLA/ODeMz2CNCROlZCjlgQ1S5Wp P6q0XQP9/r4JfTqvUwAxYpzzgP52HLy/YIAf5TiGvTxTzC3WpkotRivsq+i79w== Received: from mail-111-mta2.mxroute.com ([136.175.111.2] filter006.mxroute.com) (Authenticated sender: mN4UYu2MZsgR) by zmta1 (ZoneMTA) with ESMTPSA id 18719b21ffa000edb4.002 for (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256); Sat, 25 Mar 2023 16:55:16 +0000 X-Zone-Loop: 4cffb7bee6531d5de37af01dc4c63861cb2f4138add6 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=IWVIi6RL2FVQSpcOof4rzfziLHvppL4V8KrjsHx1M1c=; b=skhCbjGgdy4iMQu6YcWN7ccntg tOJm0kVzj1Taflm1CQXIavjIrof3nCLwBSX9VItscp3tNoe5KkeuD4TVJtW0DQDUt6FSGuluVqcMi +utKnZg+O04qD1rRU5xo/nWMMhEXdsYZ/m/V5DXybI/Xmp0Rddna2hhZ58gC837WSsw7JY6JjCrUH lUxrpPx/T66IuSh1+701btAF8yqPrZV3Sx9OK6rbSwnrjBFoiZ2NJSPh+BA3/8SU2mr2J8+1Fzd5S BhyefnWczuqdFcxfqssfY+aDvVQW7v1FGQuM8MHNd5KIrsbB4iYQhhdl83IktkZrRFMItkwRzwvPu 6hy6fGxg==; From: Jack Kamm To: Ihor Radchenko Cc: Matt , emacs-orgmode Subject: Re: [BUG] shell sessions started outside of Babel broken In-Reply-To: <87ttzhy9vm.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> Date: Sat, 25 Mar 2023 09:55:10 -0700 Message-ID: <87a6006aa9.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Authenticated-Id: jackkamm@tatersworld.org Received-SPF: pass client-ip=68.168.211.171; envelope-from=jackkamm@tatersworld.org; helo=relay2-p.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: A9DFACCAB X-Spam-Score: -8.68 X-Migadu-Spam-Score: -8.68 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: CicWJIo4SZrU --=-=-= Content-Type: text/plain 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. --=-=-= 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 --=-=-=--