aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2013-09-02 10:56:03 -0300
committerFabián Ezequiel Gallina2013-09-02 10:56:03 -0300
commit96edb677815a79f2952d49959a0d4a8be6da08d6 (patch)
treea6265bb12fca68b50d7133503ccc9ea72b3c6057 /lisp/progmodes/python.el
parent95beaef369763423111bca752a7a3c5c3d853fb0 (diff)
downloademacs-96edb677815a79f2952d49959a0d4a8be6da08d6.tar.gz
emacs-96edb677815a79f2952d49959a0d4a8be6da08d6.zip
Format code sent to Python shell for robustness.
* progmodes/python.el (python-shell-buffer-substring): New function. (python-shell-send-region, python-shell-send-buffer): Use it.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el65
1 files changed, 51 insertions, 14 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index af55854bbc4..fb2dc01c9be 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -2137,17 +2137,58 @@ Returns the output. See `python-shell-send-string-no-output'."
2137(define-obsolete-function-alias 2137(define-obsolete-function-alias
2138 'python-send-string 'python-shell-internal-send-string "24.3") 2138 'python-send-string 'python-shell-internal-send-string "24.3")
2139 2139
2140(defun python-shell-buffer-substring (start end &optional nomain)
2141 "Send buffer substring from START to END formatted for shell.
2142This is a wrapper over `buffer-substring' that takes care of
2143different transformations for the code sent to be evaluated in
2144the python shell:
2145 1. When Optional Argument NOMAIN is non-nil everything under an
2146 \"if __name__ == '__main__'\" block will be removed.
2147 2. When a subregion of the buffer is sent, it takes care of
2148 appending extra whitelines so tracebacks are correct.
2149 3. Wraps indented regions under an \"if True:\" block so the
2150 interpreter evaluates them correctly."
2151 (let ((substring (buffer-substring-no-properties start end))
2152 (fillstr (make-string (1- (line-number-at-pos start)) ?\n))
2153 (toplevel-block-p (save-excursion
2154 (goto-char start)
2155 (or (zerop (line-number-at-pos start))
2156 (progn
2157 (python-util-forward-comment 1)
2158 (zerop (current-indentation)))))))
2159 (with-temp-buffer
2160 (python-mode)
2161 (insert fillstr)
2162 (insert substring)
2163 (goto-char (point-min))
2164 (when (not toplevel-block-p)
2165 (insert "if True:")
2166 (delete-region (point) (line-end-position)))
2167 (when nomain
2168 (let* ((if-name-main-start-end
2169 (and nomain
2170 (save-excursion
2171 (when (python-nav-if-name-main)
2172 (cons (point)
2173 (progn (python-nav-forward-sexp)
2174 (point)))))))
2175 ;; Oh destructuring bind, how I miss you.
2176 (if-name-main-start (car if-name-main-start-end))
2177 (if-name-main-end (cdr if-name-main-start-end)))
2178 (when if-name-main-start-end
2179 (goto-char if-name-main-start)
2180 (delete-region if-name-main-start if-name-main-end)
2181 (insert
2182 (make-string
2183 (- (line-number-at-pos if-name-main-end)
2184 (line-number-at-pos if-name-main-start)) ?\n)))))
2185 (buffer-substring-no-properties (point-min) (point-max)))))
2186
2140(defun python-shell-send-region (start end) 2187(defun python-shell-send-region (start end)
2141 "Send the region delimited by START and END to inferior Python process." 2188 "Send the region delimited by START and END to inferior Python process."
2142 (interactive "r") 2189 (interactive "r")
2143 (python-shell-send-string 2190 (python-shell-send-string
2144 (concat 2191 (python-shell-buffer-substring start end) nil t))
2145 (let ((line-num (line-number-at-pos start)))
2146 ;; When sending a region, add blank lines for non sent code so
2147 ;; backtraces remain correct.
2148 (make-string (1- line-num) ?\n))
2149 (buffer-substring start end))
2150 nil t))
2151 2192
2152(defun python-shell-send-buffer (&optional arg) 2193(defun python-shell-send-buffer (&optional arg)
2153 "Send the entire buffer to inferior Python process. 2194 "Send the entire buffer to inferior Python process.
@@ -2156,13 +2197,9 @@ by \"if __name__== '__main__':\""
2156 (interactive "P") 2197 (interactive "P")
2157 (save-restriction 2198 (save-restriction
2158 (widen) 2199 (widen)
2159 (let ((str (buffer-substring (point-min) (point-max)))) 2200 (python-shell-send-string
2160 (and 2201 (python-shell-buffer-substring
2161 (not arg) 2202 (point-min) (point-max) (not arg)))))
2162 (setq str (replace-regexp-in-string
2163 (python-rx if-name-main)
2164 "if __name__ == '__main__ ':" str)))
2165 (python-shell-send-string str))))
2166 2203
2167(defun python-shell-send-defun (arg) 2204(defun python-shell-send-defun (arg)
2168 "Send the current defun to inferior Python process. 2205 "Send the current defun to inferior Python process.