summaryrefslogtreecommitdiff
path: root/lisp/ob-python.el
diff options
context:
space:
mode:
authorSébastien Delafond <sdelafond@gmail.com>2014-07-13 13:35:27 +0200
committerSébastien Delafond <sdelafond@gmail.com>2014-07-13 13:35:27 +0200
commite32a45ed36d6000db4b39171149072d11b77af72 (patch)
treeb5f4a7d43022c08c3298e82b3e9fc50f68be660f /lisp/ob-python.el
parent7697fa4daf3ec84f85711a84035d8f0224afd4e3 (diff)
Imported Upstream version 8.0.7
Diffstat (limited to 'lisp/ob-python.el')
-rw-r--r--lisp/ob-python.el90
1 files changed, 67 insertions, 23 deletions
diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index 71adf73..eca4c82 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -1,6 +1,6 @@
;;; ob-python.el --- org-babel functions for python evaluation
-;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
+;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
;; Authors: Eric Schulte
;; Dan Davison
@@ -28,9 +28,6 @@
;;; Code:
(require 'ob)
-(require 'ob-ref)
-(require 'ob-comint)
-(require 'ob-eval)
(eval-when-compile (require 'cl))
(declare-function org-remove-indentation "org" )
@@ -43,15 +40,38 @@
(defvar org-babel-default-header-args:python '())
-(defvar org-babel-python-command "python"
- "Name of command for executing Python code.")
+(defcustom org-babel-python-command "python"
+ "Name of the command for executing Python code."
+ :version "24.4"
+ :package-version '(Org . "8.0")
+ :group 'org-babel
+ :type 'string)
-(defvar org-babel-python-mode (if (featurep 'xemacs) 'python-mode 'python)
+(defcustom org-babel-python-mode
+ (if (or (featurep 'xemacs) (featurep 'python-mode)) 'python-mode 'python)
"Preferred python mode for use in running python interactively.
-This will typically be either 'python or 'python-mode.")
+This will typically be either 'python or 'python-mode."
+ :group 'org-babel
+ :version "24.4"
+ :package-version '(Org . "8.0")
+ :type 'function)
(defvar org-src-preserve-indentation)
+(defcustom org-babel-python-hline-to "None"
+ "Replace hlines in incoming tables with this when translating to python."
+ :group 'org-babel
+ :version "24.4"
+ :package-version '(Org . "8.0")
+ :type 'string)
+
+(defcustom org-babel-python-None-to 'hline
+ "Replace 'None' in python tables with this before returning."
+ :group 'org-babel
+ :version "24.4"
+ :package-version '(Org . "8.0")
+ :type 'string)
+
(defun org-babel-execute:python (body params)
"Execute a block of Python code with Babel.
This function is called by `org-babel-execute-src-block'."
@@ -114,7 +134,7 @@ specifying a variable of the same value."
(if (listp var)
(concat "[" (mapconcat #'org-babel-python-var-to-python var ", ") "]")
(if (equal var 'hline)
- "None"
+ org-babel-python-hline-to
(format
(if (and (stringp var) (string-match "[\n\r]" var)) "\"\"%S\"\"" "%S")
var))))
@@ -123,14 +143,34 @@ specifying a variable of the same value."
"Convert RESULTS into an appropriate elisp value.
If the results look like a list or tuple, then convert them into an
Emacs-lisp table, otherwise return the results as a string."
- (org-babel-script-escape results))
+ ((lambda (res)
+ (if (listp res)
+ (mapcar (lambda (el) (if (equal el 'None)
+ org-babel-python-None-to el))
+ res)
+ res))
+ (org-babel-script-escape results)))
-(defvar org-babel-python-buffers '((:default . nil)))
+(defvar org-babel-python-buffers '((:default . "*Python*")))
(defun org-babel-python-session-buffer (session)
"Return the buffer associated with SESSION."
(cdr (assoc session org-babel-python-buffers)))
+(defun org-babel-python-with-earmufs (session)
+ (let ((name (if (stringp session) session (format "%s" session))))
+ (if (and (string= "*" (substring name 0 1))
+ (string= "*" (substring name (- (length name) 1))))
+ name
+ (format "*%s*" name))))
+
+(defun org-babel-python-without-earmufs (session)
+ (let ((name (if (stringp session) session (format "%s" session))))
+ (if (and (string= "*" (substring name 0 1))
+ (string= "*" (substring name (- (length name) 1))))
+ (substring name 1 (- (length name) 1))
+ name)))
+
(defvar py-default-interpreter)
(defun org-babel-python-initiate-session-by-key (&optional session)
"Initiate a python session.
@@ -143,7 +183,17 @@ then create. Return the initialized session."
(cond
((and (eq 'python org-babel-python-mode)
(fboundp 'run-python)) ; python.el
- (run-python))
+ (if (version< "24.1" emacs-version)
+ (progn
+ (unless python-buffer
+ (setq python-buffer (org-babel-python-with-earmufs session)))
+ (let ((python-shell-buffer-name
+ (org-babel-python-without-earmufs python-buffer)))
+ (run-python
+ (if (member system-type '(cygwin windows-nt ms-dos))
+ (concat org-babel-python-command " -i")
+ org-babel-python-command))))
+ (run-python)))
((and (eq 'python-mode org-babel-python-mode)
(fboundp 'py-shell)) ; python-mode.el
;; Make sure that py-which-bufname is initialized, as otherwise
@@ -158,7 +208,7 @@ then create. Return the initialized session."
(concat "Python-" (symbol-name session))))
(py-which-bufname bufname))
(py-shell)
- (setq python-buffer (concat "*" bufname "*"))))
+ (setq python-buffer (org-babel-python-with-earmufs bufname))))
(t
(error "No function available for running an inferior Python")))
(setq org-babel-python-buffers
@@ -204,11 +254,8 @@ If RESULT-TYPE equals 'output then return standard output as a
string. If RESULT-TYPE equals 'value then return the value of the
last statement in BODY, as elisp."
((lambda (raw)
- (if (or (member "code" result-params)
- (member "pp" result-params)
- (and (member "output" result-params)
- (not (member "table" result-params))))
- raw
+ (org-babel-result-cond result-params
+ raw
(org-babel-python-table-or-string (org-babel-trim raw))))
(case result-type
(output (org-babel-eval org-babel-python-command
@@ -257,11 +304,8 @@ last statement in BODY, as elisp."
(funcall send-wait))))
((lambda (results)
(unless (string= (substring org-babel-python-eoe-indicator 1 -1) results)
- (if (or (member "code" result-params)
- (member "pp" result-params)
- (and (member "output" result-params)
- (not (member "table" result-params))))
- results
+ (org-babel-result-cond result-params
+ results
(org-babel-python-table-or-string results))))
(case result-type
(output