aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/org/ob-python.el
diff options
context:
space:
mode:
authorKyle Meyer2022-11-29 23:05:53 -0500
committerKyle Meyer2022-11-29 23:05:53 -0500
commit0625651e8a61c9effc31ff771f15885a3a37c6e6 (patch)
treedb4c09e8ef119ad4a9a4028c5e615fd58d2dee69 /lisp/org/ob-python.el
parentedd64e64a389e0f0e6ce670846d4fae79a9d8b35 (diff)
downloademacs-0625651e8a61c9effc31ff771f15885a3a37c6e6.tar.gz
emacs-0625651e8a61c9effc31ff771f15885a3a37c6e6.zip
Update to Org 9.6-3-ga4d38e
Diffstat (limited to 'lisp/org/ob-python.el')
-rw-r--r--lisp/org/ob-python.el30
1 files changed, 25 insertions, 5 deletions
diff --git a/lisp/org/ob-python.el b/lisp/org/ob-python.el
index 3e9049e73fb..1829ab149a2 100644
--- a/lisp/org/ob-python.el
+++ b/lisp/org/ob-python.el
@@ -6,7 +6,7 @@
6;; Dan Davison 6;; Dan Davison
7;; Maintainer: Jack Kamm <jackkamm@gmail.com> 7;; Maintainer: Jack Kamm <jackkamm@gmail.com>
8;; Keywords: literate programming, reproducible research 8;; Keywords: literate programming, reproducible research
9;; Homepage: https://orgmode.org 9;; URL: https://orgmode.org
10 10
11;; This file is part of GNU Emacs. 11;; This file is part of GNU Emacs.
12 12
@@ -28,12 +28,16 @@
28;; Org-Babel support for evaluating python source code. 28;; Org-Babel support for evaluating python source code.
29 29
30;;; Code: 30;;; Code:
31
32(require 'org-macs)
33(org-assert-version)
34
31(require 'ob) 35(require 'ob)
32(require 'org-macs) 36(require 'org-macs)
33(require 'python) 37(require 'python)
34 38
35(declare-function py-shell "ext:python-mode" (&rest args)) 39(declare-function py-shell "ext:python-mode" (&rest args))
36(declare-function py-toggle-shells "ext:python-mode" (arg)) 40(declare-function py-choose-shell "ext:python-mode" (&optional shell))
37(declare-function py-shell-send-string "ext:python-mode" (strg &optional process)) 41(declare-function py-shell-send-string "ext:python-mode" (strg &optional process))
38 42
39(defvar org-babel-tangle-lang-exts) 43(defvar org-babel-tangle-lang-exts)
@@ -178,9 +182,10 @@ Emacs-lisp table, otherwise return the results as a string."
178 (substring name 1 (- (length name) 1)) 182 (substring name 1 (- (length name) 1))
179 name))) 183 name)))
180 184
181(defvar py-default-interpreter)
182(defvar py-which-bufname) 185(defvar py-which-bufname)
183(defvar python-shell-buffer-name) 186(defvar python-shell-buffer-name)
187(defvar-local org-babel-python--initialized nil
188 "Flag used to mark that python session has been initialized.")
184(defun org-babel-python-initiate-session-by-key (&optional session) 189(defun org-babel-python-initiate-session-by-key (&optional session)
185 "Initiate a python session. 190 "Initiate a python session.
186If there is not a current inferior-process-buffer in SESSION 191If there is not a current inferior-process-buffer in SESSION
@@ -198,14 +203,20 @@ then create. Return the initialized session."
198 (let ((python-shell-buffer-name 203 (let ((python-shell-buffer-name
199 (org-babel-python-without-earmuffs py-buffer))) 204 (org-babel-python-without-earmuffs py-buffer)))
200 (run-python cmd) 205 (run-python cmd)
201 (sleep-for 0 10))) 206 (with-current-buffer py-buffer
207 (add-hook
208 'python-shell-first-prompt-hook
209 (lambda ()
210 (setq-local org-babel-python--initialized t)
211 (message "I am running!!!"))
212 nil 'local))))
202 ((and (eq 'python-mode org-babel-python-mode) 213 ((and (eq 'python-mode org-babel-python-mode)
203 (fboundp 'py-shell)) ; python-mode.el 214 (fboundp 'py-shell)) ; python-mode.el
204 (require 'python-mode) 215 (require 'python-mode)
205 ;; Make sure that py-which-bufname is initialized, as otherwise 216 ;; Make sure that py-which-bufname is initialized, as otherwise
206 ;; it will be overwritten the first time a Python buffer is 217 ;; it will be overwritten the first time a Python buffer is
207 ;; created. 218 ;; created.
208 (py-toggle-shells py-default-interpreter) 219 (py-choose-shell)
209 ;; `py-shell' creates a buffer whose name is the value of 220 ;; `py-shell' creates a buffer whose name is the value of
210 ;; `py-which-bufname' with '*'s at the beginning and end 221 ;; `py-which-bufname' with '*'s at the beginning and end
211 (let* ((bufname (if (and py-buffer (buffer-live-p py-buffer)) 222 (let* ((bufname (if (and py-buffer (buffer-live-p py-buffer))
@@ -217,6 +228,15 @@ then create. Return the initialized session."
217 (py-shell nil nil t org-babel-python-command py-buffer nil nil t nil))) 228 (py-shell nil nil t org-babel-python-command py-buffer nil nil t nil)))
218 (t 229 (t
219 (error "No function available for running an inferior Python"))) 230 (error "No function available for running an inferior Python")))
231 ;; Wait until Python initializes.
232 (if (eq 'python org-babel-python-mode) ; python.el
233 ;; This is more reliable compared to
234 ;; `org-babel-comint-wait-for-output' as python may emit
235 ;; multiple prompts during initialization.
236 (with-current-buffer py-buffer
237 (while (not org-babel-python--initialized)
238 (org-babel-comint-wait-for-output py-buffer)))
239 (org-babel-comint-wait-for-output py-buffer))
220 (setq org-babel-python-buffers 240 (setq org-babel-python-buffers
221 (cons (cons session py-buffer) 241 (cons (cons session py-buffer)
222 (assq-delete-all session org-babel-python-buffers))) 242 (assq-delete-all session org-babel-python-buffers)))