aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/org/ob-python.el
diff options
context:
space:
mode:
authorBastien Guerry2012-09-30 17:14:59 +0200
committerBastien Guerry2012-09-30 17:14:59 +0200
commit8223b1d23361b74ede10bac47974ce7803804380 (patch)
tree3a2491c5193fed1bef14acd45092c0b9736fa5d6 /lisp/org/ob-python.el
parent163227893c97b5b41039ea9d5ceadb7e5b2d570c (diff)
downloademacs-8223b1d23361b74ede10bac47974ce7803804380.tar.gz
emacs-8223b1d23361b74ede10bac47974ce7803804380.zip
Sync Org 7.9.2 from the commit tagged "release_7.9.2" in Org's Git repo.
Diffstat (limited to 'lisp/org/ob-python.el')
-rw-r--r--lisp/org/ob-python.el59
1 files changed, 30 insertions, 29 deletions
diff --git a/lisp/org/ob-python.el b/lisp/org/ob-python.el
index 348248f35cf..71adf73073b 100644
--- a/lisp/org/ob-python.el
+++ b/lisp/org/ob-python.el
@@ -44,7 +44,7 @@
44(defvar org-babel-default-header-args:python '()) 44(defvar org-babel-default-header-args:python '())
45 45
46(defvar org-babel-python-command "python" 46(defvar org-babel-python-command "python"
47 "Name of command for executing python code.") 47 "Name of command for executing Python code.")
48 48
49(defvar org-babel-python-mode (if (featurep 'xemacs) 'python-mode 'python) 49(defvar org-babel-python-mode (if (featurep 'xemacs) 'python-mode 'python)
50 "Preferred python mode for use in running python interactively. 50 "Preferred python mode for use in running python interactively.
@@ -99,7 +99,7 @@ VARS contains resolved variable references"
99;; helper functions 99;; helper functions
100 100
101(defun org-babel-variable-assignments:python (params) 101(defun org-babel-variable-assignments:python (params)
102 "Return list of python statements assigning the block's variables" 102 "Return a list of Python statements assigning the block's variables."
103 (mapcar 103 (mapcar
104 (lambda (pair) 104 (lambda (pair)
105 (format "%s=%s" 105 (format "%s=%s"
@@ -160,7 +160,7 @@ then create. Return the initialized session."
160 (py-shell) 160 (py-shell)
161 (setq python-buffer (concat "*" bufname "*")))) 161 (setq python-buffer (concat "*" bufname "*"))))
162 (t 162 (t
163 (error "No function available for running an inferior python."))) 163 (error "No function available for running an inferior Python")))
164 (setq org-babel-python-buffers 164 (setq org-babel-python-buffers
165 (cons (cons session python-buffer) 165 (cons (cons session python-buffer)
166 (assq-delete-all session org-babel-python-buffers))) 166 (assq-delete-all session org-babel-python-buffers)))
@@ -190,7 +190,7 @@ open('%s', 'w').write( pprint.pformat(main()) )")
190 190
191(defun org-babel-python-evaluate 191(defun org-babel-python-evaluate
192 (session body &optional result-type result-params preamble) 192 (session body &optional result-type result-params preamble)
193 "Evaluate BODY as python code." 193 "Evaluate BODY as Python code."
194 (if session 194 (if session
195 (org-babel-python-evaluate-session 195 (org-babel-python-evaluate-session
196 session body result-type result-params) 196 session body result-type result-params)
@@ -201,7 +201,7 @@ open('%s', 'w').write( pprint.pformat(main()) )")
201 (body &optional result-type result-params preamble) 201 (body &optional result-type result-params preamble)
202 "Evaluate BODY in external python process. 202 "Evaluate BODY in external python process.
203If RESULT-TYPE equals 'output then return standard output as a 203If RESULT-TYPE equals 'output then return standard output as a
204string. If RESULT-TYPE equals 'value then return the value of the 204string. If RESULT-TYPE equals 'value then return the value of the
205last statement in BODY, as elisp." 205last statement in BODY, as elisp."
206 ((lambda (raw) 206 ((lambda (raw)
207 (if (or (member "code" result-params) 207 (if (or (member "code" result-params)
@@ -236,24 +236,25 @@ last statement in BODY, as elisp."
236 (session body &optional result-type result-params) 236 (session body &optional result-type result-params)
237 "Pass BODY to the Python process in SESSION. 237 "Pass BODY to the Python process in SESSION.
238If RESULT-TYPE equals 'output then return standard output as a 238If RESULT-TYPE equals 'output then return standard output as a
239string. If RESULT-TYPE equals 'value then return the value of the 239string. If RESULT-TYPE equals 'value then return the value of the
240last statement in BODY, as elisp." 240last statement in BODY, as elisp."
241 (flet ((send-wait () (comint-send-input nil t) (sleep-for 0 5)) 241 (let* ((send-wait (lambda () (comint-send-input nil t) (sleep-for 0 5)))
242 (dump-last-value 242 (dump-last-value
243 (tmp-file pp) 243 (lambda
244 (mapc 244 (tmp-file pp)
245 (lambda (statement) (insert statement) (send-wait)) 245 (mapc
246 (if pp 246 (lambda (statement) (insert statement) (funcall send-wait))
247 (list 247 (if pp
248 "import pprint" 248 (list
249 (format "open('%s', 'w').write(pprint.pformat(_))" 249 "import pprint"
250 (org-babel-process-file-name tmp-file 'noquote))) 250 (format "open('%s', 'w').write(pprint.pformat(_))"
251 (list (format "open('%s', 'w').write(str(_))" 251 (org-babel-process-file-name tmp-file 'noquote)))
252 (org-babel-process-file-name tmp-file 'noquote)))))) 252 (list (format "open('%s', 'w').write(str(_))"
253 (input-body (body) 253 (org-babel-process-file-name tmp-file 'noquote)))))))
254 (mapc (lambda (line) (insert line) (send-wait)) 254 (input-body (lambda (body)
255 (split-string body "[\r\n]")) 255 (mapc (lambda (line) (insert line) (funcall send-wait))
256 (send-wait))) 256 (split-string body "[\r\n]"))
257 (funcall send-wait))))
257 ((lambda (results) 258 ((lambda (results)
258 (unless (string= (substring org-babel-python-eoe-indicator 1 -1) results) 259 (unless (string= (substring org-babel-python-eoe-indicator 1 -1) results)
259 (if (or (member "code" result-params) 260 (if (or (member "code" result-params)
@@ -269,25 +270,25 @@ last statement in BODY, as elisp."
269 (butlast 270 (butlast
270 (org-babel-comint-with-output 271 (org-babel-comint-with-output
271 (session org-babel-python-eoe-indicator t body) 272 (session org-babel-python-eoe-indicator t body)
272 (input-body body) 273 (funcall input-body body)
273 (send-wait) (send-wait) 274 (funcall send-wait) (funcall send-wait)
274 (insert org-babel-python-eoe-indicator) 275 (insert org-babel-python-eoe-indicator)
275 (send-wait)) 276 (funcall send-wait))
276 2) "\n")) 277 2) "\n"))
277 (value 278 (value
278 (let ((tmp-file (org-babel-temp-file "python-"))) 279 (let ((tmp-file (org-babel-temp-file "python-")))
279 (org-babel-comint-with-output 280 (org-babel-comint-with-output
280 (session org-babel-python-eoe-indicator nil body) 281 (session org-babel-python-eoe-indicator nil body)
281 (let ((comint-process-echoes nil)) 282 (let ((comint-process-echoes nil))
282 (input-body body) 283 (funcall input-body body)
283 (dump-last-value tmp-file (member "pp" result-params)) 284 (funcall dump-last-value tmp-file (member "pp" result-params))
284 (send-wait) (send-wait) 285 (funcall send-wait) (funcall send-wait)
285 (insert org-babel-python-eoe-indicator) 286 (insert org-babel-python-eoe-indicator)
286 (send-wait))) 287 (funcall send-wait)))
287 (org-babel-eval-read-file tmp-file))))))) 288 (org-babel-eval-read-file tmp-file)))))))
288 289
289(defun org-babel-python-read-string (string) 290(defun org-babel-python-read-string (string)
290 "Strip 's from around python string" 291 "Strip 's from around Python string."
291 (if (string-match "^'\\([^\000]+\\)'$" string) 292 (if (string-match "^'\\([^\000]+\\)'$" string)
292 (match-string 1 string) 293 (match-string 1 string)
293 string)) 294 string))