diff options
| author | Rasmus | 2017-06-21 13:20:20 +0200 |
|---|---|---|
| committer | Rasmus | 2017-06-22 11:54:18 +0200 |
| commit | 5cecd275820df825c51bf9a27fcc7e35f30ff273 (patch) | |
| tree | b3f72e63953613d565e6d5a35bec97f158eb603c /lisp/org/ob-python.el | |
| parent | 386a3da920482b8cb3e962fb944d135c8a770e26 (diff) | |
| download | emacs-5cecd275820df825c51bf9a27fcc7e35f30ff273.tar.gz emacs-5cecd275820df825c51bf9a27fcc7e35f30ff273.zip | |
Update Org to v9.0.9
Please see etc/ORG-NEWS for details.
Diffstat (limited to 'lisp/org/ob-python.el')
| -rw-r--r-- | lisp/org/ob-python.el | 111 |
1 files changed, 57 insertions, 54 deletions
diff --git a/lisp/org/ob-python.el b/lisp/org/ob-python.el index dfad47bf9e0..302f8bd451e 100644 --- a/lisp/org/ob-python.el +++ b/lisp/org/ob-python.el | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | ;;; ob-python.el --- org-babel functions for python evaluation | 1 | ;;; ob-python.el --- Babel Functions for Python -*- lexical-binding: t; -*- |
| 2 | 2 | ||
| 3 | ;; Copyright (C) 2009-2017 Free Software Foundation, Inc. | 3 | ;; Copyright (C) 2009-2017 Free Software Foundation, Inc. |
| 4 | 4 | ||
| @@ -28,9 +28,9 @@ | |||
| 28 | 28 | ||
| 29 | ;;; Code: | 29 | ;;; Code: |
| 30 | (require 'ob) | 30 | (require 'ob) |
| 31 | (eval-when-compile (require 'cl)) | ||
| 32 | 31 | ||
| 33 | (declare-function org-remove-indentation "org" ) | 32 | (declare-function org-remove-indentation "org" ) |
| 33 | (declare-function org-trim "org" (s &optional keep-lead)) | ||
| 34 | (declare-function py-shell "ext:python-mode" (&optional argprompt)) | 34 | (declare-function py-shell "ext:python-mode" (&optional argprompt)) |
| 35 | (declare-function py-toggle-shells "ext:python-mode" (arg)) | 35 | (declare-function py-toggle-shells "ext:python-mode" (arg)) |
| 36 | (declare-function run-python "ext:python" (&optional cmd dedicated show)) | 36 | (declare-function run-python "ext:python" (&optional cmd dedicated show)) |
| @@ -48,9 +48,9 @@ | |||
| 48 | :type 'string) | 48 | :type 'string) |
| 49 | 49 | ||
| 50 | (defcustom org-babel-python-mode | 50 | (defcustom org-babel-python-mode |
| 51 | (if (or (featurep 'xemacs) (featurep 'python-mode)) 'python-mode 'python) | 51 | (if (featurep 'python-mode) 'python-mode 'python) |
| 52 | "Preferred python mode for use in running python interactively. | 52 | "Preferred python mode for use in running python interactively. |
| 53 | This will typically be either 'python or 'python-mode." | 53 | This will typically be either `python' or `python-mode'." |
| 54 | :group 'org-babel | 54 | :group 'org-babel |
| 55 | :version "24.4" | 55 | :version "24.4" |
| 56 | :package-version '(Org . "8.0") | 56 | :package-version '(Org . "8.0") |
| @@ -73,13 +73,16 @@ This will typically be either 'python or 'python-mode." | |||
| 73 | (defun org-babel-execute:python (body params) | 73 | (defun org-babel-execute:python (body params) |
| 74 | "Execute a block of Python code with Babel. | 74 | "Execute a block of Python code with Babel. |
| 75 | This function is called by `org-babel-execute-src-block'." | 75 | This function is called by `org-babel-execute-src-block'." |
| 76 | (let* ((session (org-babel-python-initiate-session | 76 | (let* ((org-babel-python-command |
| 77 | (cdr (assoc :session params)))) | 77 | (or (cdr (assq :python params)) |
| 78 | (result-params (cdr (assoc :result-params params))) | 78 | org-babel-python-command)) |
| 79 | (result-type (cdr (assoc :result-type params))) | 79 | (session (org-babel-python-initiate-session |
| 80 | (cdr (assq :session params)))) | ||
| 81 | (result-params (cdr (assq :result-params params))) | ||
| 82 | (result-type (cdr (assq :result-type params))) | ||
| 80 | (return-val (when (and (eq result-type 'value) (not session)) | 83 | (return-val (when (and (eq result-type 'value) (not session)) |
| 81 | (cdr (assoc :return params)))) | 84 | (cdr (assq :return params)))) |
| 82 | (preamble (cdr (assoc :preamble params))) | 85 | (preamble (cdr (assq :preamble params))) |
| 83 | (full-body | 86 | (full-body |
| 84 | (org-babel-expand-body:generic | 87 | (org-babel-expand-body:generic |
| 85 | (concat body (if return-val (format "\nreturn %s" return-val) "")) | 88 | (concat body (if return-val (format "\nreturn %s" return-val) "")) |
| @@ -88,10 +91,10 @@ This function is called by `org-babel-execute-src-block'." | |||
| 88 | session full-body result-type result-params preamble))) | 91 | session full-body result-type result-params preamble))) |
| 89 | (org-babel-reassemble-table | 92 | (org-babel-reassemble-table |
| 90 | result | 93 | result |
| 91 | (org-babel-pick-name (cdr (assoc :colname-names params)) | 94 | (org-babel-pick-name (cdr (assq :colname-names params)) |
| 92 | (cdr (assoc :colnames params))) | 95 | (cdr (assq :colnames params))) |
| 93 | (org-babel-pick-name (cdr (assoc :rowname-names params)) | 96 | (org-babel-pick-name (cdr (assq :rowname-names params)) |
| 94 | (cdr (assoc :rownames params)))))) | 97 | (cdr (assq :rownames params)))))) |
| 95 | 98 | ||
| 96 | (defun org-babel-prep-session:python (session params) | 99 | (defun org-babel-prep-session:python (session params) |
| 97 | "Prepare SESSION according to the header arguments in PARAMS. | 100 | "Prepare SESSION according to the header arguments in PARAMS. |
| @@ -123,7 +126,7 @@ VARS contains resolved variable references" | |||
| 123 | (format "%s=%s" | 126 | (format "%s=%s" |
| 124 | (car pair) | 127 | (car pair) |
| 125 | (org-babel-python-var-to-python (cdr pair)))) | 128 | (org-babel-python-var-to-python (cdr pair)))) |
| 126 | (mapcar #'cdr (org-babel-get-header params :var)))) | 129 | (org-babel--get-vars params))) |
| 127 | 130 | ||
| 128 | (defun org-babel-python-var-to-python (var) | 131 | (defun org-babel-python-var-to-python (var) |
| 129 | "Convert an elisp value to a python variable. | 132 | "Convert an elisp value to a python variable. |
| @@ -131,7 +134,7 @@ Convert an elisp value, VAR, into a string of python source code | |||
| 131 | specifying a variable of the same value." | 134 | specifying a variable of the same value." |
| 132 | (if (listp var) | 135 | (if (listp var) |
| 133 | (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]") | 136 | (concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]") |
| 134 | (if (equal var 'hline) | 137 | (if (eq var 'hline) |
| 135 | org-babel-python-hline-to | 138 | org-babel-python-hline-to |
| 136 | (format | 139 | (format |
| 137 | (if (and (stringp var) (string-match "[\n\r]" var)) "\"\"%S\"\"" "%S") | 140 | (if (and (stringp var) (string-match "[\n\r]" var)) "\"\"%S\"\"" "%S") |
| @@ -143,7 +146,7 @@ If the results look like a list or tuple, then convert them into an | |||
| 143 | Emacs-lisp table, otherwise return the results as a string." | 146 | Emacs-lisp table, otherwise return the results as a string." |
| 144 | (let ((res (org-babel-script-escape results))) | 147 | (let ((res (org-babel-script-escape results))) |
| 145 | (if (listp res) | 148 | (if (listp res) |
| 146 | (mapcar (lambda (el) (if (equal el 'None) | 149 | (mapcar (lambda (el) (if (eq el 'None) |
| 147 | org-babel-python-None-to el)) | 150 | org-babel-python-None-to el)) |
| 148 | res) | 151 | res) |
| 149 | res))) | 152 | res))) |
| @@ -214,7 +217,7 @@ then create. Return the initialized session." | |||
| 214 | (assq-delete-all session org-babel-python-buffers))) | 217 | (assq-delete-all session org-babel-python-buffers))) |
| 215 | session))) | 218 | session))) |
| 216 | 219 | ||
| 217 | (defun org-babel-python-initiate-session (&optional session params) | 220 | (defun org-babel-python-initiate-session (&optional session _params) |
| 218 | "Create a session named SESSION according to PARAMS." | 221 | "Create a session named SESSION according to PARAMS." |
| 219 | (unless (string= session "none") | 222 | (unless (string= session "none") |
| 220 | (org-babel-python-session-buffer | 223 | (org-babel-python-session-buffer |
| @@ -222,13 +225,13 @@ then create. Return the initialized session." | |||
| 222 | 225 | ||
| 223 | (defvar org-babel-python-eoe-indicator "'org_babel_python_eoe'" | 226 | (defvar org-babel-python-eoe-indicator "'org_babel_python_eoe'" |
| 224 | "A string to indicate that evaluation has completed.") | 227 | "A string to indicate that evaluation has completed.") |
| 225 | (defvar org-babel-python-wrapper-method | 228 | (defconst org-babel-python-wrapper-method |
| 226 | " | 229 | " |
| 227 | def main(): | 230 | def main(): |
| 228 | %s | 231 | %s |
| 229 | 232 | ||
| 230 | open('%s', 'w').write( str(main()) )") | 233 | open('%s', 'w').write( str(main()) )") |
| 231 | (defvar org-babel-python-pp-wrapper-method | 234 | (defconst org-babel-python-pp-wrapper-method |
| 232 | " | 235 | " |
| 233 | import pprint | 236 | import pprint |
| 234 | def main(): | 237 | def main(): |
| @@ -246,42 +249,41 @@ open('%s', 'w').write( pprint.pformat(main()) )") | |||
| 246 | body result-type result-params preamble))) | 249 | body result-type result-params preamble))) |
| 247 | 250 | ||
| 248 | (defun org-babel-python-evaluate-external-process | 251 | (defun org-babel-python-evaluate-external-process |
| 249 | (body &optional result-type result-params preamble) | 252 | (body &optional result-type result-params preamble) |
| 250 | "Evaluate BODY in external python process. | 253 | "Evaluate BODY in external python process. |
| 251 | If RESULT-TYPE equals 'output then return standard output as a | 254 | If RESULT-TYPE equals `output' then return standard output as a |
| 252 | string. If RESULT-TYPE equals 'value then return the value of the | 255 | string. If RESULT-TYPE equals `value' then return the value of the |
| 253 | last statement in BODY, as elisp." | 256 | last statement in BODY, as elisp." |
| 254 | (let ((raw | 257 | (let ((raw |
| 255 | (case result-type | 258 | (pcase result-type |
| 256 | (output (org-babel-eval org-babel-python-command | 259 | (`output (org-babel-eval org-babel-python-command |
| 257 | (concat (if preamble (concat preamble "\n")) | 260 | (concat (if preamble (concat preamble "\n")) |
| 258 | body))) | 261 | body))) |
| 259 | (value (let ((tmp-file (org-babel-temp-file "python-"))) | 262 | (`value (let ((tmp-file (org-babel-temp-file "python-"))) |
| 260 | (org-babel-eval | 263 | (org-babel-eval |
| 261 | org-babel-python-command | 264 | org-babel-python-command |
| 262 | (concat | 265 | (concat |
| 263 | (if preamble (concat preamble "\n") "") | 266 | (if preamble (concat preamble "\n") "") |
| 264 | (format | 267 | (format |
| 265 | (if (member "pp" result-params) | 268 | (if (member "pp" result-params) |
| 266 | org-babel-python-pp-wrapper-method | 269 | org-babel-python-pp-wrapper-method |
| 267 | org-babel-python-wrapper-method) | 270 | org-babel-python-wrapper-method) |
| 268 | (mapconcat | 271 | (mapconcat |
| 269 | (lambda (line) (format "\t%s" line)) | 272 | (lambda (line) (format "\t%s" line)) |
| 270 | (split-string | 273 | (split-string (org-remove-indentation (org-trim body)) |
| 271 | (org-remove-indentation | 274 | "[\r\n]") |
| 272 | (org-babel-trim body)) | 275 | "\n") |
| 273 | "[\r\n]") "\n") | 276 | (org-babel-process-file-name tmp-file 'noquote)))) |
| 274 | (org-babel-process-file-name tmp-file 'noquote)))) | 277 | (org-babel-eval-read-file tmp-file)))))) |
| 275 | (org-babel-eval-read-file tmp-file)))))) | ||
| 276 | (org-babel-result-cond result-params | 278 | (org-babel-result-cond result-params |
| 277 | raw | 279 | raw |
| 278 | (org-babel-python-table-or-string (org-babel-trim raw))))) | 280 | (org-babel-python-table-or-string (org-trim raw))))) |
| 279 | 281 | ||
| 280 | (defun org-babel-python-evaluate-session | 282 | (defun org-babel-python-evaluate-session |
| 281 | (session body &optional result-type result-params) | 283 | (session body &optional result-type result-params) |
| 282 | "Pass BODY to the Python process in SESSION. | 284 | "Pass BODY to the Python process in SESSION. |
| 283 | If RESULT-TYPE equals 'output then return standard output as a | 285 | If RESULT-TYPE equals `output' then return standard output as a |
| 284 | string. If RESULT-TYPE equals 'value then return the value of the | 286 | string. If RESULT-TYPE equals `value' then return the value of the |
| 285 | last statement in BODY, as elisp." | 287 | last statement in BODY, as elisp." |
| 286 | (let* ((send-wait (lambda () (comint-send-input nil t) (sleep-for 0 5))) | 288 | (let* ((send-wait (lambda () (comint-send-input nil t) (sleep-for 0 5))) |
| 287 | (dump-last-value | 289 | (dump-last-value |
| @@ -302,10 +304,10 @@ last statement in BODY, as elisp." | |||
| 302 | (split-string body "[\r\n]")) | 304 | (split-string body "[\r\n]")) |
| 303 | (funcall send-wait))) | 305 | (funcall send-wait))) |
| 304 | (results | 306 | (results |
| 305 | (case result-type | 307 | (pcase result-type |
| 306 | (output | 308 | (`output |
| 307 | (mapconcat | 309 | (mapconcat |
| 308 | #'org-babel-trim | 310 | #'org-trim |
| 309 | (butlast | 311 | (butlast |
| 310 | (org-babel-comint-with-output | 312 | (org-babel-comint-with-output |
| 311 | (session org-babel-python-eoe-indicator t body) | 313 | (session org-babel-python-eoe-indicator t body) |
| @@ -314,7 +316,7 @@ last statement in BODY, as elisp." | |||
| 314 | (insert org-babel-python-eoe-indicator) | 316 | (insert org-babel-python-eoe-indicator) |
| 315 | (funcall send-wait)) | 317 | (funcall send-wait)) |
| 316 | 2) "\n")) | 318 | 2) "\n")) |
| 317 | (value | 319 | (`value |
| 318 | (let ((tmp-file (org-babel-temp-file "python-"))) | 320 | (let ((tmp-file (org-babel-temp-file "python-"))) |
| 319 | (org-babel-comint-with-output | 321 | (org-babel-comint-with-output |
| 320 | (session org-babel-python-eoe-indicator nil body) | 322 | (session org-babel-python-eoe-indicator nil body) |
| @@ -332,9 +334,10 @@ last statement in BODY, as elisp." | |||
| 332 | (org-babel-python-table-or-string results))))) | 334 | (org-babel-python-table-or-string results))))) |
| 333 | 335 | ||
| 334 | (defun org-babel-python-read-string (string) | 336 | (defun org-babel-python-read-string (string) |
| 335 | "Strip 's from around Python string." | 337 | "Strip \\='s from around Python string." |
| 336 | (if (string-match "^'\\([^\000]+\\)'$" string) | 338 | (if (and (string-prefix-p "'" string) |
| 337 | (match-string 1 string) | 339 | (string-suffix-p "'" string)) |
| 340 | (substring string 1 -1) | ||
| 338 | string)) | 341 | string)) |
| 339 | 342 | ||
| 340 | (provide 'ob-python) | 343 | (provide 'ob-python) |