aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorJoakim Verona2014-12-23 17:03:28 +0100
committerJoakim Verona2014-12-23 17:03:28 +0100
commit5f46725992bf26f887483c14c63c03f5b5794f34 (patch)
tree2896a2b98a65851eaddba81d0138fe5647005b08 /lisp/progmodes/python.el
parent1d8b8a2d8f75d1e3ec9eb109e98720bd9a197bec (diff)
parente3040f2aee768655198dd6f979a1ff3a72d17d16 (diff)
downloademacs-5f46725992bf26f887483c14c63c03f5b5794f34.tar.gz
emacs-5f46725992bf26f887483c14c63c03f5b5794f34.zip
Merge branch 'master' into xwidget
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el502
1 files changed, 300 insertions, 202 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index d1dd9bef4bc..35e24e14e1c 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -94,13 +94,13 @@
94;; python-shell-interpreter-args 94;; python-shell-interpreter-args
95;; "-i C:\\Python27\\Scripts\\ipython-script.py") 95;; "-i C:\\Python27\\Scripts\\ipython-script.py")
96 96
97;; If you are experiencing missing or delayed output in your shells, 97;; Missing or delayed output used to happen due to differences between
98;; that's likely caused by your Operating System's pipe buffering 98;; Operating Systems' pipe buffering (e.g. CPython 3.3.4 in Windows 7.
99;; (e.g. this is known to happen running CPython 3.3.4 in Windows 7.
100;; See URL `http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17304'). To 99;; See URL `http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17304'). To
101;; fix this, using CPython's "-u" commandline argument or setting the 100;; avoid this, the `python-shell-unbuffered' defaults to non-nil and
102;; "PYTHONUNBUFFERED" environment variable should help: See URL 101;; controls whether `python-shell-calculate-process-environment'
103;; `https://docs.python.org/3/using/cmdline.html#cmdoption-u'. 102;; should set the "PYTHONUNBUFFERED" environment variable on startup:
103;; See URL `https://docs.python.org/3/using/cmdline.html#cmdoption-u'.
104 104
105;; The interaction relies upon having prompts for input (e.g. ">>> " 105;; The interaction relies upon having prompts for input (e.g. ">>> "
106;; and "... " in standard Python shell) and output (e.g. "Out[1]: " in 106;; and "... " in standard Python shell) and output (e.g. "Out[1]: " in
@@ -158,13 +158,13 @@
158;; (python-shell-exec-path . ("/path/to/env/bin/")) 158;; (python-shell-exec-path . ("/path/to/env/bin/"))
159 159
160;; Since the above is cumbersome and can be programmatically 160;; Since the above is cumbersome and can be programmatically
161;; calculated, the variable `python-shell-virtualenv-path' is 161;; calculated, the variable `python-shell-virtualenv-root' is
162;; provided. When this variable is set with the path of the 162;; provided. When this variable is set with the path of the
163;; virtualenv to use, `process-environment' and `exec-path' get proper 163;; virtualenv to use, `process-environment' and `exec-path' get proper
164;; values in order to run shells inside the specified virtualenv. So 164;; values in order to run shells inside the specified virtualenv. So
165;; the following will achieve the same as the previous example: 165;; the following will achieve the same as the previous example:
166 166
167;; (setq python-shell-virtualenv-path "/path/to/env/") 167;; (setq python-shell-virtualenv-root "/path/to/env/")
168 168
169;; Also the `python-shell-extra-pythonpaths' variable have been 169;; Also the `python-shell-extra-pythonpaths' variable have been
170;; introduced as simple way of adding paths to the PYTHONPATH without 170;; introduced as simple way of adding paths to the PYTHONPATH without
@@ -217,11 +217,13 @@
217;; `python-imenu-format-parent-item-jump-label-function' variables for 217;; `python-imenu-format-parent-item-jump-label-function' variables for
218;; changing the way labels are formatted in the tree version. 218;; changing the way labels are formatted in the tree version.
219 219
220;; If you used python-mode.el you probably will miss auto-indentation 220;; If you used python-mode.el you may miss auto-indentation when
221;; when inserting newlines. To achieve the same behavior you have 221;; inserting newlines. To achieve the same behavior you have two
222;; two options: 222;; options:
223 223
224;; 1) Use GNU/Emacs' standard binding for `newline-and-indent': C-j. 224;; 1) Enable the minor-mode `electric-indent-mode' (enabled by
225;; default) and use RET. If this mode is disabled use
226;; `newline-and-indent', bound to C-j.
225 227
226;; 2) Add the following hook in your .emacs: 228;; 2) Add the following hook in your .emacs:
227 229
@@ -457,6 +459,23 @@ The type returned can be `comment', `string' or `paren'."
457 'python-info-ppss-comment-or-string-p 459 'python-info-ppss-comment-or-string-p
458 #'python-syntax-comment-or-string-p "24.3") 460 #'python-syntax-comment-or-string-p "24.3")
459 461
462(defun python-docstring-at-p (pos)
463 "Check to see if there is a docstring at POS."
464 (save-excursion
465 (goto-char pos)
466 (if (looking-at-p "'''\\|\"\"\"")
467 (progn
468 (python-nav-backward-statement)
469 (looking-at "\\`\\|class \\|def "))
470 nil)))
471
472(defun python-font-lock-syntactic-face-function (state)
473 (if (nth 3 state)
474 (if (python-docstring-at-p (nth 8 state))
475 font-lock-doc-face
476 font-lock-string-face)
477 font-lock-comment-face))
478
460(defvar python-font-lock-keywords 479(defvar python-font-lock-keywords
461 ;; Keywords 480 ;; Keywords
462 `(,(rx symbol-start 481 `(,(rx symbol-start
@@ -545,7 +564,7 @@ The type returned can be `comment', `string' or `paren'."
545 (res nil)) 564 (res nil))
546 (while (and (setq res (re-search-forward re limit t)) 565 (while (and (setq res (re-search-forward re limit t))
547 (or (python-syntax-context 'paren) 566 (or (python-syntax-context 'paren)
548 (equal (char-after (point-marker)) ?=)))) 567 (equal (char-after (point)) ?=))))
549 res)) 568 res))
550 (1 font-lock-variable-name-face nil nil)) 569 (1 font-lock-variable-name-face nil nil))
551 ;; support for a, b, c = (1, 2, 3) 570 ;; support for a, b, c = (1, 2, 3)
@@ -670,7 +689,8 @@ It makes underscores and dots word constituent chars.")
670 "Current indentation level `python-indent-line-function' is using.") 689 "Current indentation level `python-indent-line-function' is using.")
671 690
672(defvar python-indent-levels '(0) 691(defvar python-indent-levels '(0)
673 "Levels of indentation available for `python-indent-line-function'.") 692 "Levels of indentation available for `python-indent-line-function'.
693Can also be `noindent' if automatic indentation can't be used.")
674 694
675(defun python-indent-guess-indent-offset () 695(defun python-indent-guess-indent-offset ()
676 "Guess and set `python-indent-offset' for the current buffer." 696 "Guess and set `python-indent-offset' for the current buffer."
@@ -791,7 +811,9 @@ START is the buffer position where the sexp starts."
791 start)))) 811 start))))
792 812
793(defun python-indent-calculate-indentation () 813(defun python-indent-calculate-indentation ()
794 "Calculate correct indentation offset for the current line." 814 "Calculate correct indentation offset for the current line.
815Returns `noindent' if the indentation does not depend on Python syntax,
816such as in strings."
795 (let* ((indentation-context (python-indent-context)) 817 (let* ((indentation-context (python-indent-context))
796 (context-status (car indentation-context)) 818 (context-status (car indentation-context))
797 (context-start (cdr indentation-context))) 819 (context-start (cdr indentation-context)))
@@ -841,9 +863,7 @@ START is the buffer position where the sexp starts."
841 ;; When inside of a string, do nothing. just use the current 863 ;; When inside of a string, do nothing. just use the current
842 ;; indentation. XXX: perhaps it would be a good idea to 864 ;; indentation. XXX: perhaps it would be a good idea to
843 ;; invoke standard text indentation here 865 ;; invoke standard text indentation here
844 (`inside-string 866 (`inside-string 'noindent)
845 (goto-char context-start)
846 (current-indentation))
847 ;; After backslash we have several possibilities. 867 ;; After backslash we have several possibilities.
848 (`after-backslash 868 (`after-backslash
849 (cond 869 (cond
@@ -964,15 +984,22 @@ START is the buffer position where the sexp starts."
964 984
965(defun python-indent-calculate-levels () 985(defun python-indent-calculate-levels ()
966 "Calculate `python-indent-levels' and reset `python-indent-current-level'." 986 "Calculate `python-indent-levels' and reset `python-indent-current-level'."
967 (if (not (python-info-dedenter-statement-p)) 987 (if (or (python-info-continuation-line-p)
968 (let* ((indentation (python-indent-calculate-indentation)) 988 (not (python-info-dedenter-statement-p)))
969 (remainder (% indentation python-indent-offset)) 989 ;; XXX: This asks for a refactor. Even if point is on a
970 (steps (/ (- indentation remainder) python-indent-offset))) 990 ;; dedenter statement, it could be multiline and in that case
971 (setq python-indent-levels (list 0)) 991 ;; the continuation lines should be indented with normal rules.
972 (dotimes (step steps) 992 (let* ((indentation (python-indent-calculate-indentation)))
973 (push (* python-indent-offset (1+ step)) python-indent-levels)) 993 (if (not (numberp indentation))
974 (when (not (eq 0 remainder)) 994 (setq python-indent-levels indentation)
975 (push (+ (* python-indent-offset steps) remainder) python-indent-levels))) 995 (let* ((remainder (% indentation python-indent-offset))
996 (steps (/ (- indentation remainder) python-indent-offset)))
997 (setq python-indent-levels (list 0))
998 (dotimes (step steps)
999 (push (* python-indent-offset (1+ step)) python-indent-levels))
1000 (when (not (eq 0 remainder))
1001 (push (+ (* python-indent-offset steps) remainder)
1002 python-indent-levels)))))
976 (setq python-indent-levels 1003 (setq python-indent-levels
977 (or 1004 (or
978 (mapcar (lambda (pos) 1005 (mapcar (lambda (pos)
@@ -981,8 +1008,9 @@ START is the buffer position where the sexp starts."
981 (current-indentation))) 1008 (current-indentation)))
982 (python-info-dedenter-opening-block-positions)) 1009 (python-info-dedenter-opening-block-positions))
983 (list 0)))) 1010 (list 0))))
984 (setq python-indent-current-level (1- (length python-indent-levels)) 1011 (when (listp python-indent-levels)
985 python-indent-levels (nreverse python-indent-levels))) 1012 (setq python-indent-current-level (1- (length python-indent-levels))
1013 python-indent-levels (nreverse python-indent-levels))))
986 1014
987(defun python-indent-toggle-levels () 1015(defun python-indent-toggle-levels ()
988 "Toggle `python-indent-current-level' over `python-indent-levels'." 1016 "Toggle `python-indent-current-level' over `python-indent-levels'."
@@ -1010,28 +1038,30 @@ in the variable `python-indent-levels'. Afterwards it sets the
1010variable `python-indent-current-level' correctly so offset is 1038variable `python-indent-current-level' correctly so offset is
1011equal to 1039equal to
1012 (nth python-indent-current-level python-indent-levels)" 1040 (nth python-indent-current-level python-indent-levels)"
1013 (or 1041 (if (and (or (and (memq this-command python-indent-trigger-commands)
1014 (and (or (and (memq this-command python-indent-trigger-commands) 1042 (eq last-command this-command))
1015 (eq last-command this-command)) 1043 force-toggle)
1016 force-toggle) 1044 (not (equal python-indent-levels '(0))))
1017 (not (equal python-indent-levels '(0))) 1045 (if (listp python-indent-levels)
1018 (or (python-indent-toggle-levels) t)) 1046 (python-indent-toggle-levels))
1019 (python-indent-calculate-levels)) 1047 (python-indent-calculate-levels))
1020 (let* ((starting-pos (point-marker)) 1048 (if (eq python-indent-levels 'noindent)
1021 (indent-ending-position 1049 python-indent-levels
1022 (+ (line-beginning-position) (current-indentation))) 1050 (let* ((starting-pos (point-marker))
1023 (follow-indentation-p 1051 (indent-ending-position
1024 (or (bolp) 1052 (+ (line-beginning-position) (current-indentation)))
1025 (and (<= (line-beginning-position) starting-pos) 1053 (follow-indentation-p
1026 (>= indent-ending-position starting-pos)))) 1054 (or (bolp)
1027 (next-indent (nth python-indent-current-level python-indent-levels))) 1055 (and (<= (line-beginning-position) starting-pos)
1028 (unless (= next-indent (current-indentation)) 1056 (>= indent-ending-position starting-pos))))
1029 (beginning-of-line) 1057 (next-indent (nth python-indent-current-level python-indent-levels)))
1030 (delete-horizontal-space) 1058 (unless (= next-indent (current-indentation))
1031 (indent-to next-indent) 1059 (beginning-of-line)
1032 (goto-char starting-pos)) 1060 (delete-horizontal-space)
1033 (and follow-indentation-p (back-to-indentation))) 1061 (indent-to next-indent)
1034 (python-info-dedenter-opening-block-message)) 1062 (goto-char starting-pos))
1063 (and follow-indentation-p (back-to-indentation)))
1064 (python-info-dedenter-opening-block-message)))
1035 1065
1036(defun python-indent-line-function () 1066(defun python-indent-line-function ()
1037 "`indent-line-function' for Python mode. 1067 "`indent-line-function' for Python mode.
@@ -1042,9 +1072,9 @@ See `python-indent-line' for details."
1042 "De-indent current line." 1072 "De-indent current line."
1043 (interactive "*") 1073 (interactive "*")
1044 (when (and (not (python-syntax-comment-or-string-p)) 1074 (when (and (not (python-syntax-comment-or-string-p))
1045 (<= (point-marker) (save-excursion 1075 (<= (point) (save-excursion
1046 (back-to-indentation) 1076 (back-to-indentation)
1047 (point-marker))) 1077 (point)))
1048 (> (current-column) 0)) 1078 (> (current-column) 0))
1049 (python-indent-line t) 1079 (python-indent-line t)
1050 t)) 1080 t))
@@ -1070,24 +1100,34 @@ Called from a program, START and END specify the region to indent."
1070 (or (bolp) (forward-line 1)) 1100 (or (bolp) (forward-line 1))
1071 (while (< (point) end) 1101 (while (< (point) end)
1072 (or (and (bolp) (eolp)) 1102 (or (and (bolp) (eolp))
1073 (let (word) 1103 (when (and
1074 (forward-line -1) 1104 ;; Skip if previous line is empty or a comment.
1075 (back-to-indentation) 1105 (save-excursion
1076 (setq word (current-word)) 1106 (let ((line-is-comment-p
1077 (forward-line 1) 1107 (python-info-current-line-comment-p)))
1078 (when (and word 1108 (forward-line -1)
1079 ;; Don't mess with strings, unless it's the 1109 (not
1080 ;; enclosing set of quotes. 1110 (or (and (python-info-current-line-comment-p)
1081 (or (not (python-syntax-context 'string)) 1111 ;; Unless this line is a comment too.
1082 (eq 1112 (not line-is-comment-p))
1083 (syntax-after 1113 (python-info-current-line-empty-p)))))
1084 (+ (1- (point)) 1114 ;; Don't mess with strings, unless it's the
1085 (current-indentation) 1115 ;; enclosing set of quotes.
1086 (python-syntax-count-quotes (char-after) (point)))) 1116 (or (not (python-syntax-context 'string))
1087 (string-to-syntax "|")))) 1117 (eq
1088 (beginning-of-line) 1118 (syntax-after
1089 (delete-horizontal-space) 1119 (+ (1- (point))
1090 (indent-to (python-indent-calculate-indentation))))) 1120 (current-indentation)
1121 (python-syntax-count-quotes (char-after) (point))))
1122 (string-to-syntax "|")))
1123 ;; Skip if current line is a block start, a
1124 ;; dedenter or block ender.
1125 (save-excursion
1126 (back-to-indentation)
1127 (not (looking-at
1128 (python-rx
1129 (or block-start dedenter block-ender))))))
1130 (python-indent-line)))
1091 (forward-line 1)) 1131 (forward-line 1))
1092 (move-marker end nil)))) 1132 (move-marker end nil))))
1093 1133
@@ -1156,15 +1196,21 @@ the line will be re-indented automatically if needed."
1156 (save-excursion 1196 (save-excursion
1157 (goto-char (line-beginning-position)) 1197 (goto-char (line-beginning-position))
1158 (let ((indentation (python-indent-calculate-indentation))) 1198 (let ((indentation (python-indent-calculate-indentation)))
1159 (when (< (current-indentation) indentation) 1199 (when (and (numberp indentation) (< (current-indentation) indentation))
1160 (indent-line-to indentation))))) 1200 (indent-line-to indentation)))))
1161 ;; Electric colon 1201 ;; Electric colon
1162 ((and (eq ?: last-command-event) 1202 ((and (eq ?: last-command-event)
1163 (memq ?: electric-indent-chars) 1203 (memq ?: electric-indent-chars)
1164 (not current-prefix-arg) 1204 (not current-prefix-arg)
1205 ;; Trigger electric colon only at end of line
1165 (eolp) 1206 (eolp)
1207 ;; Avoid re-indenting on extra colon
1166 (not (equal ?: (char-before (1- (point))))) 1208 (not (equal ?: (char-before (1- (point)))))
1167 (not (python-syntax-comment-or-string-p))) 1209 (not (python-syntax-comment-or-string-p))
1210 ;; Never re-indent at beginning of defun
1211 (not (save-excursion
1212 (python-nav-beginning-of-statement)
1213 (python-info-looking-at-beginning-of-defun))))
1168 (python-indent-line))))) 1214 (python-indent-line)))))
1169 1215
1170 1216
@@ -1795,7 +1841,7 @@ It should not contain a caret (^) at the beginning."
1795 :type 'string) 1841 :type 'string)
1796 1842
1797(define-obsolete-variable-alias 1843(define-obsolete-variable-alias
1798 'python-shell-enable-font-lock 'python-shell-font-lock-enable "24.5") 1844 'python-shell-enable-font-lock 'python-shell-font-lock-enable "25.1")
1799 1845
1800(defcustom python-shell-font-lock-enable t 1846(defcustom python-shell-font-lock-enable t
1801 "Should syntax highlighting be enabled in the Python shell buffer? 1847 "Should syntax highlighting be enabled in the Python shell buffer?
@@ -1804,6 +1850,14 @@ Restart the Python shell after changing this variable for it to take effect."
1804 :group 'python 1850 :group 'python
1805 :safe 'booleanp) 1851 :safe 'booleanp)
1806 1852
1853(defcustom python-shell-unbuffered t
1854 "Should shell output be unbuffered?.
1855When non-nil, this may prevent delayed and missing output in the
1856Python shell. See commentary for details."
1857 :type 'boolean
1858 :group 'python
1859 :safe 'booleanp)
1860
1807(defcustom python-shell-process-environment nil 1861(defcustom python-shell-process-environment nil
1808 "List of environment variables for Python shell. 1862 "List of environment variables for Python shell.
1809This variable follows the same rules as `process-environment' 1863This variable follows the same rules as `process-environment'
@@ -1832,7 +1886,7 @@ default `exec-path'."
1832 :group 'python 1886 :group 'python
1833 :safe 'listp) 1887 :safe 'listp)
1834 1888
1835(defcustom python-shell-virtualenv-path nil 1889(defcustom python-shell-virtualenv-root nil
1836 "Path to virtualenv root. 1890 "Path to virtualenv root.
1837This variable, when set to a string, makes the values stored in 1891This variable, when set to a string, makes the values stored in
1838`python-shell-process-environment' and `python-shell-exec-path' 1892`python-shell-process-environment' and `python-shell-exec-path'
@@ -1842,6 +1896,9 @@ virtualenv."
1842 :group 'python 1896 :group 'python
1843 :safe 'stringp) 1897 :safe 'stringp)
1844 1898
1899(define-obsolete-variable-alias
1900 'python-shell-virtualenv-path 'python-shell-virtualenv-root "25.1")
1901
1845(defcustom python-shell-setup-codes '(python-shell-completion-setup-code 1902(defcustom python-shell-setup-codes '(python-shell-completion-setup-code
1846 python-ffap-setup-code 1903 python-ffap-setup-code
1847 python-eldoc-setup-code) 1904 python-eldoc-setup-code)
@@ -1905,7 +1962,7 @@ detection and just returns nil."
1905 (let ((code-file (python-shell--save-temp-file code))) 1962 (let ((code-file (python-shell--save-temp-file code)))
1906 ;; Use `process-file' as it is remote-host friendly. 1963 ;; Use `process-file' as it is remote-host friendly.
1907 (process-file 1964 (process-file
1908 (executable-find python-shell-interpreter) 1965 python-shell-interpreter
1909 code-file 1966 code-file
1910 '(t nil) 1967 '(t nil)
1911 nil 1968 nil
@@ -2058,33 +2115,47 @@ uniqueness for different types of configurations."
2058 (mapconcat #'identity python-shell-process-environment "") 2115 (mapconcat #'identity python-shell-process-environment "")
2059 (mapconcat #'identity python-shell-extra-pythonpaths "") 2116 (mapconcat #'identity python-shell-extra-pythonpaths "")
2060 (mapconcat #'identity python-shell-exec-path "") 2117 (mapconcat #'identity python-shell-exec-path "")
2061 (or python-shell-virtualenv-path "") 2118 (or python-shell-virtualenv-root "")
2062 (mapconcat #'identity python-shell-exec-path ""))))) 2119 (mapconcat #'identity python-shell-exec-path "")))))
2063 2120
2064(defun python-shell-parse-command () 2121(defun python-shell-calculate-command ()
2065 "Calculate the string used to execute the inferior Python process." 2122 "Calculate the string used to execute the inferior Python process."
2066 (let ((process-environment (python-shell-calculate-process-environment)) 2123 (let ((exec-path (python-shell-calculate-exec-path)))
2067 (exec-path (python-shell-calculate-exec-path))) 2124 ;; `exec-path' gets tweaked so that virtualenv's specific
2125 ;; `python-shell-interpreter' absolute path can be found by
2126 ;; `executable-find'.
2068 (format "%s %s" 2127 (format "%s %s"
2069 (executable-find python-shell-interpreter) 2128 ;; FIXME: Why executable-find?
2129 (shell-quote-argument
2130 (executable-find python-shell-interpreter))
2070 python-shell-interpreter-args))) 2131 python-shell-interpreter-args)))
2071 2132
2133(define-obsolete-function-alias
2134 'python-shell-parse-command
2135 #'python-shell-calculate-command "25.1")
2136
2137(defun python-shell-calculate-pythonpath ()
2138 "Calculate the PYTHONPATH using `python-shell-extra-pythonpaths'."
2139 (let ((pythonpath (getenv "PYTHONPATH"))
2140 (extra (mapconcat 'identity
2141 python-shell-extra-pythonpaths
2142 path-separator)))
2143 (if pythonpath
2144 (concat extra path-separator pythonpath)
2145 extra)))
2146
2072(defun python-shell-calculate-process-environment () 2147(defun python-shell-calculate-process-environment ()
2073 "Calculate process environment given `python-shell-virtualenv-path'." 2148 "Calculate process environment given `python-shell-virtualenv-root'."
2074 (let ((process-environment (append 2149 (let ((process-environment (append
2075 python-shell-process-environment 2150 python-shell-process-environment
2076 process-environment nil)) 2151 process-environment nil))
2077 (virtualenv (if python-shell-virtualenv-path 2152 (virtualenv (if python-shell-virtualenv-root
2078 (directory-file-name python-shell-virtualenv-path) 2153 (directory-file-name python-shell-virtualenv-root)
2079 nil))) 2154 nil)))
2155 (when python-shell-unbuffered
2156 (setenv "PYTHONUNBUFFERED" "1"))
2080 (when python-shell-extra-pythonpaths 2157 (when python-shell-extra-pythonpaths
2081 (setenv "PYTHONPATH" 2158 (setenv "PYTHONPATH" (python-shell-calculate-pythonpath)))
2082 (format "%s%s%s"
2083 (mapconcat 'identity
2084 python-shell-extra-pythonpaths
2085 path-separator)
2086 path-separator
2087 (or (getenv "PYTHONPATH") ""))))
2088 (if (not virtualenv) 2159 (if (not virtualenv)
2089 process-environment 2160 process-environment
2090 (setenv "PYTHONHOME" nil) 2161 (setenv "PYTHONHOME" nil)
@@ -2095,13 +2166,14 @@ uniqueness for different types of configurations."
2095 process-environment)) 2166 process-environment))
2096 2167
2097(defun python-shell-calculate-exec-path () 2168(defun python-shell-calculate-exec-path ()
2098 "Calculate exec path given `python-shell-virtualenv-path'." 2169 "Calculate exec path given `python-shell-virtualenv-root'."
2099 (let ((path (append python-shell-exec-path 2170 (let ((path (append
2100 exec-path nil))) 2171 ;; Use nil as the tail so that the list is a full copy,
2101 (if (not python-shell-virtualenv-path) 2172 ;; this is a paranoid safeguard for side-effects.
2173 python-shell-exec-path exec-path nil)))
2174 (if (not python-shell-virtualenv-root)
2102 path 2175 path
2103 (cons (format "%s/bin" 2176 (cons (expand-file-name "bin" python-shell-virtualenv-root)
2104 (directory-file-name python-shell-virtualenv-path))
2105 path)))) 2177 path))))
2106 2178
2107(defvar python-shell--package-depth 10) 2179(defvar python-shell--package-depth 10)
@@ -2129,6 +2201,27 @@ uniqueness for different types of configurations."
2129 directory package package) 2201 directory package package)
2130 (python-shell-get-process))) 2202 (python-shell-get-process)))
2131 2203
2204(defun python-shell-accept-process-output (process &optional timeout regexp)
2205 "Accept PROCESS output with TIMEOUT until REGEXP is found.
2206Optional argument TIMEOUT is the timeout argument to
2207`accept-process-output' calls. Optional argument REGEXP
2208overrides the regexp to match the end of output, defaults to
2209`comint-prompt-regexp.'. Returns non-nil when output was
2210properly captured.
2211
2212This utility is useful in situations where the output may be
2213received in chunks, since `accept-process-output' gives no
2214guarantees they will be grabbed in a single call. An example use
2215case for this would be the CPython shell start-up, where the
2216banner and the initial prompt are received separately."
2217 (let ((regexp (or regexp comint-prompt-regexp)))
2218 (catch 'found
2219 (while t
2220 (when (not (accept-process-output process timeout))
2221 (throw 'found nil))
2222 (when (looking-back regexp)
2223 (throw 'found t))))))
2224
2132(defun python-shell-comint-end-of-output-p (output) 2225(defun python-shell-comint-end-of-output-p (output)
2133 "Return non-nil if OUTPUT is ends with input prompt." 2226 "Return non-nil if OUTPUT is ends with input prompt."
2134 (string-match 2227 (string-match
@@ -2146,7 +2239,7 @@ uniqueness for different types of configurations."
2146(define-obsolete-function-alias 2239(define-obsolete-function-alias
2147 'python-comint-output-filter-function 2240 'python-comint-output-filter-function
2148 'ansi-color-filter-apply 2241 'ansi-color-filter-apply
2149 "24.5") 2242 "25.1")
2150 2243
2151(defun python-comint-postoutput-scroll-to-bottom (output) 2244(defun python-comint-postoutput-scroll-to-bottom (output)
2152 "Faster version of `comint-postoutput-scroll-to-bottom'. 2245 "Faster version of `comint-postoutput-scroll-to-bottom'.
@@ -2188,7 +2281,7 @@ Signals an error if no shell buffer is available for current buffer."
2188 (when (and python-shell--font-lock-buffer 2281 (when (and python-shell--font-lock-buffer
2189 (buffer-live-p python-shell--font-lock-buffer)) 2282 (buffer-live-p python-shell--font-lock-buffer))
2190 (kill-buffer python-shell--font-lock-buffer) 2283 (kill-buffer python-shell--font-lock-buffer)
2191 (when (eq major-mode 'inferior-python-mode) 2284 (when (derived-mode-p 'inferior-python-mode)
2192 (setq python-shell--font-lock-buffer nil))))) 2285 (setq python-shell--font-lock-buffer nil)))))
2193 2286
2194(defmacro python-shell-font-lock-with-font-lock-buffer (&rest body) 2287(defmacro python-shell-font-lock-with-font-lock-buffer (&rest body)
@@ -2205,7 +2298,7 @@ also `with-current-buffer'."
2205 (set-buffer python-shell--font-lock-buffer) 2298 (set-buffer python-shell--font-lock-buffer)
2206 (set (make-local-variable 'delay-mode-hooks) t) 2299 (set (make-local-variable 'delay-mode-hooks) t)
2207 (let ((python-indent-guess-indent-offset nil)) 2300 (let ((python-indent-guess-indent-offset nil))
2208 (when (not (eq major-mode 'python-mode)) 2301 (when (not (derived-mode-p 'python-mode))
2209 (python-mode)) 2302 (python-mode))
2210 ,@body)))) 2303 ,@body))))
2211 2304
@@ -2357,8 +2450,8 @@ variable.
2357 (set (make-local-variable 'python-shell--prompt-calculated-input-regexp) nil) 2450 (set (make-local-variable 'python-shell--prompt-calculated-input-regexp) nil)
2358 (set (make-local-variable 'python-shell--prompt-calculated-output-regexp) nil) 2451 (set (make-local-variable 'python-shell--prompt-calculated-output-regexp) nil)
2359 (python-shell-prompt-set-calculated-regexps) 2452 (python-shell-prompt-set-calculated-regexps)
2360 (setq comint-prompt-regexp python-shell--prompt-calculated-input-regexp 2453 (setq comint-prompt-regexp python-shell--prompt-calculated-input-regexp)
2361 comint-prompt-read-only t) 2454 (set (make-local-variable 'comint-prompt-read-only) t)
2362 (setq mode-line-process '(":%s")) 2455 (setq mode-line-process '(":%s"))
2363 (set (make-local-variable 'comint-output-filter-functions) 2456 (set (make-local-variable 'comint-output-filter-functions)
2364 '(ansi-color-process-output 2457 '(ansi-color-process-output
@@ -2366,12 +2459,8 @@ variable.
2366 python-comint-postoutput-scroll-to-bottom)) 2459 python-comint-postoutput-scroll-to-bottom))
2367 (set (make-local-variable 'compilation-error-regexp-alist) 2460 (set (make-local-variable 'compilation-error-regexp-alist)
2368 python-shell-compilation-regexp-alist) 2461 python-shell-compilation-regexp-alist)
2369 (define-key inferior-python-mode-map [remap complete-symbol]
2370 'completion-at-point)
2371 (add-hook 'completion-at-point-functions 2462 (add-hook 'completion-at-point-functions
2372 'python-shell-completion-at-point nil 'local) 2463 #'python-shell-completion-at-point nil 'local)
2373 (add-to-list (make-local-variable 'comint-dynamic-complete-functions)
2374 'python-shell-completion-at-point)
2375 (define-key inferior-python-mode-map "\t" 2464 (define-key inferior-python-mode-map "\t"
2376 'python-shell-completion-complete-or-indent) 2465 'python-shell-completion-complete-or-indent)
2377 (make-local-variable 'python-pdbtrack-buffers-to-kill) 2466 (make-local-variable 'python-pdbtrack-buffers-to-kill)
@@ -2380,13 +2469,8 @@ variable.
2380 (when python-shell-font-lock-enable 2469 (when python-shell-font-lock-enable
2381 (python-shell-font-lock-turn-on)) 2470 (python-shell-font-lock-turn-on))
2382 (compilation-shell-minor-mode 1) 2471 (compilation-shell-minor-mode 1)
2383 ;; Ensure all the output is accepted before running any hooks. 2472 (python-shell-accept-process-output
2384 (accept-process-output (get-buffer-process (current-buffer))) 2473 (get-buffer-process (current-buffer))))
2385 ;; At this point, all process output should have been received, but
2386 ;; on GNU/Linux, calling `python-shell-internal-send-string' without
2387 ;; a running internal shell fails to grab output properly unless
2388 ;; this `sit-for' is in place.
2389 (sit-for 0.1 t))
2390 2474
2391(defun python-shell-make-comint (cmd proc-name &optional pop internal) 2475(defun python-shell-make-comint (cmd proc-name &optional pop internal)
2392 "Create a Python shell comint buffer. 2476 "Create a Python shell comint buffer.
@@ -2427,16 +2511,17 @@ killed."
2427 proc-buffer-name))) 2511 proc-buffer-name)))
2428 2512
2429;;;###autoload 2513;;;###autoload
2430(defun run-python (cmd &optional dedicated show) 2514(defun run-python (&optional cmd dedicated show)
2431 "Run an inferior Python process. 2515 "Run an inferior Python process.
2432Input and output via buffer named after 2516Input and output via buffer named after
2433`python-shell-buffer-name'. If there is a process already 2517`python-shell-buffer-name'. If there is a process already
2434running in that buffer, just switch to it. 2518running in that buffer, just switch to it.
2435 2519
2436With argument, allows you to define CMD so you can edit the 2520Argument CMD defaults to `python-shell-calculate-command' return
2437command used to call the interpreter and define DEDICATED, so a 2521value. When called interactively with `prefix-arg', it allows
2438dedicated process for the current buffer is open. When numeric 2522the user to edit such value and choose whether the interpreter
2439prefix arg is other than 0 or 4 do not SHOW. 2523should be DEDICATED for the current buffer. When numeric prefix
2524arg is other than 0 or 4 do not SHOW.
2440 2525
2441Runs the hook `inferior-python-mode-hook' after 2526Runs the hook `inferior-python-mode-hook' after
2442`comint-mode-hook' is run. (Type \\[describe-mode] in the 2527`comint-mode-hook' is run. (Type \\[describe-mode] in the
@@ -2444,12 +2529,13 @@ process buffer for a list of commands.)"
2444 (interactive 2529 (interactive
2445 (if current-prefix-arg 2530 (if current-prefix-arg
2446 (list 2531 (list
2447 (read-shell-command "Run Python: " (python-shell-parse-command)) 2532 (read-shell-command "Run Python: " (python-shell-calculate-command))
2448 (y-or-n-p "Make dedicated process? ") 2533 (y-or-n-p "Make dedicated process? ")
2449 (= (prefix-numeric-value current-prefix-arg) 4)) 2534 (= (prefix-numeric-value current-prefix-arg) 4))
2450 (list (python-shell-parse-command) nil t))) 2535 (list (python-shell-calculate-command) nil t)))
2451 (python-shell-make-comint 2536 (python-shell-make-comint
2452 cmd (python-shell-get-process-name dedicated) show) 2537 (or cmd (python-shell-calculate-command))
2538 (python-shell-get-process-name dedicated) show)
2453 dedicated) 2539 dedicated)
2454 2540
2455(defun run-python-internal () 2541(defun run-python-internal ()
@@ -2471,13 +2557,13 @@ startup."
2471 (inferior-python-mode-hook nil)) 2557 (inferior-python-mode-hook nil))
2472 (get-buffer-process 2558 (get-buffer-process
2473 (python-shell-make-comint 2559 (python-shell-make-comint
2474 (python-shell-parse-command) 2560 (python-shell-calculate-command)
2475 (python-shell-internal-get-process-name) nil t)))) 2561 (python-shell-internal-get-process-name) nil t))))
2476 2562
2477(defun python-shell-get-buffer () 2563(defun python-shell-get-buffer ()
2478 "Return inferior Python buffer for current buffer. 2564 "Return inferior Python buffer for current buffer.
2479If current buffer is in `inferior-python-mode', return it." 2565If current buffer is in `inferior-python-mode', return it."
2480 (if (eq major-mode 'inferior-python-mode) 2566 (if (derived-mode-p 'inferior-python-mode)
2481 (current-buffer) 2567 (current-buffer)
2482 (let* ((dedicated-proc-name (python-shell-get-process-name t)) 2568 (let* ((dedicated-proc-name (python-shell-get-process-name t))
2483 (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) 2569 (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name))
@@ -2790,8 +2876,8 @@ This function takes the list of setup code to send from the
2790 python-shell-setup-codes 2876 python-shell-setup-codes
2791 "\n\n") 2877 "\n\n")
2792 "\n\nprint ('python.el: sent setup code')"))) 2878 "\n\nprint ('python.el: sent setup code')")))
2793 (python-shell-send-string code) 2879 (python-shell-send-string code process)
2794 (accept-process-output process))) 2880 (python-shell-accept-process-output process)))
2795 2881
2796(add-hook 'inferior-python-mode-hook 2882(add-hook 'inferior-python-mode-hook
2797 #'python-shell-send-setup-code) 2883 #'python-shell-send-setup-code)
@@ -2801,25 +2887,30 @@ This function takes the list of setup code to send from the
2801 2887
2802(defcustom python-shell-completion-setup-code 2888(defcustom python-shell-completion-setup-code
2803 "try: 2889 "try:
2804 import readline, rlcompleter 2890 import __builtin__
2805except ImportError: 2891except ImportError:
2892 # Python 3
2893 import builtins as __builtin__
2894try:
2895 import readline, rlcompleter
2896except:
2806 def __PYTHON_EL_get_completions(text): 2897 def __PYTHON_EL_get_completions(text):
2807 return [] 2898 return []
2808else: 2899else:
2809 def __PYTHON_EL_get_completions(text): 2900 def __PYTHON_EL_get_completions(text):
2901 builtins = dir(__builtin__)
2810 completions = [] 2902 completions = []
2811 try: 2903 try:
2812 splits = text.split() 2904 splits = text.split()
2813 is_module = splits and splits[0] in ('from', 'import') 2905 is_module = splits and splits[0] in ('from', 'import')
2814 is_ipython = getattr( 2906 is_ipython = ('__IPYTHON__' in builtins or
2815 __builtins__, '__IPYTHON__', 2907 '__IPYTHON__active' in builtins)
2816 getattr(__builtins__, '__IPYTHON__active', False))
2817 if is_module: 2908 if is_module:
2818 from IPython.core.completerlib import module_completion 2909 from IPython.core.completerlib import module_completion
2819 completions = module_completion(text.strip()) 2910 completions = module_completion(text.strip())
2820 elif is_ipython and getattr(__builtins__, '__IP', None): 2911 elif is_ipython and '__IP' in builtins:
2821 completions = __IP.complete(text) 2912 completions = __IP.complete(text)
2822 elif is_ipython and getattr(__builtins__, 'get_ipython', None): 2913 elif is_ipython and 'get_ipython' in builtins:
2823 completions = get_ipython().Completer.all_completions(text) 2914 completions = get_ipython().Completer.all_completions(text)
2824 else: 2915 else:
2825 i = 0 2916 i = 0
@@ -2850,52 +2941,44 @@ the full statement in the case of imports."
2850 "24.4" 2941 "24.4"
2851 "Completion string code must also autocomplete modules.") 2942 "Completion string code must also autocomplete modules.")
2852 2943
2853(defcustom python-shell-completion-pdb-string-code 2944(define-obsolete-variable-alias
2854 "';'.join(globals().keys() + locals().keys())" 2945 'python-shell-completion-pdb-string-code
2855 "Python code used to get completions separated by semicolons for [i]pdb." 2946 'python-shell-completion-string-code
2856 :type 'string 2947 "25.1"
2857 :group 'python) 2948 "Completion string code must work for (i)pdb.")
2858 2949
2859(defun python-shell-completion-get-completions (process line input) 2950(defun python-shell-completion-get-completions (process import input)
2860 "Do completion at point for PROCESS. 2951 "Do completion at point using PROCESS for IMPORT or INPUT.
2861LINE is used to detect the context on how to complete given INPUT." 2952When IMPORT is non-nil takes precedence over INPUT for
2862 (let* ((prompt 2953completion."
2863 ;; Get last prompt of the inferior process buffer (this 2954 (with-current-buffer (process-buffer process)
2864 ;; intentionally avoids using `comint-last-prompt' because 2955 (let* ((prompt
2865 ;; of incompatibilities with Emacs 24.x). 2956 (let ((prompt-boundaries (python-util-comint-last-prompt)))
2866 (with-current-buffer (process-buffer process)
2867 (save-excursion
2868 (buffer-substring-no-properties 2957 (buffer-substring-no-properties
2869 (- (point) (length line)) 2958 (car prompt-boundaries) (cdr prompt-boundaries))))
2870 (progn 2959 (completion-code
2871 (re-search-backward "^") 2960 ;; Check whether a prompt matches a pdb string, an import
2872 (python-util-forward-comment) 2961 ;; statement or just the standard prompt and use the
2873 (point)))))) 2962 ;; correct python-shell-completion-*-code string
2874 (completion-code 2963 (cond ((and (string-match
2875 ;; Check whether a prompt matches a pdb string, an import 2964 (concat "^" python-shell-prompt-pdb-regexp) prompt))
2876 ;; statement or just the standard prompt and use the 2965 ;; Since there are no guarantees the user will remain
2877 ;; correct python-shell-completion-*-code string 2966 ;; in the same context where completion code was sent
2878 (cond ((and (> (length python-shell-completion-pdb-string-code) 0) 2967 ;; (e.g. user steps into a function), safeguard
2879 (string-match 2968 ;; resending completion setup continuously.
2880 (concat "^" python-shell-prompt-pdb-regexp) prompt)) 2969 (concat python-shell-completion-setup-code
2881 python-shell-completion-pdb-string-code) 2970 "\nprint (" python-shell-completion-string-code ")"))
2882 ((string-match 2971 ((string-match
2883 python-shell--prompt-calculated-input-regexp prompt) 2972 python-shell--prompt-calculated-input-regexp prompt)
2884 python-shell-completion-string-code) 2973 python-shell-completion-string-code)
2885 (t nil))) 2974 (t nil)))
2886 (input 2975 (subject (or import input)))
2887 (if (string-match 2976 (and completion-code
2888 (python-rx (+ space) (or "from" "import") space) 2977 (> (length input) 0)
2889 line)
2890 line
2891 input)))
2892 (and completion-code
2893 (> (length input) 0)
2894 (with-current-buffer (process-buffer process)
2895 (let ((completions 2978 (let ((completions
2896 (python-util-strip-string 2979 (python-util-strip-string
2897 (python-shell-send-string-no-output 2980 (python-shell-send-string-no-output
2898 (format completion-code input) process)))) 2981 (format completion-code subject) process))))
2899 (and (> (length completions) 2) 2982 (and (> (length completions) 2)
2900 (split-string completions 2983 (split-string completions
2901 "^'\\|^\"\\|;\\|'$\\|\"$" t))))))) 2984 "^'\\|^\"\\|;\\|'$\\|\"$" t)))))))
@@ -2905,14 +2988,20 @@ LINE is used to detect the context on how to complete given INPUT."
2905Optional argument PROCESS forces completions to be retrieved 2988Optional argument PROCESS forces completions to be retrieved
2906using that one instead of current buffer's process." 2989using that one instead of current buffer's process."
2907 (setq process (or process (get-buffer-process (current-buffer)))) 2990 (setq process (or process (get-buffer-process (current-buffer))))
2908 (let* ((start 2991 (let* ((last-prompt-end (cdr (python-util-comint-last-prompt)))
2992 (import-statement
2993 (when (string-match-p
2994 (rx (* space) word-start (or "from" "import") word-end space)
2995 (buffer-substring-no-properties last-prompt-end (point)))
2996 (buffer-substring-no-properties last-prompt-end (point))))
2997 (start
2909 (save-excursion 2998 (save-excursion
2910 (if (not (re-search-backward 2999 (if (not (re-search-backward
2911 (python-rx 3000 (python-rx
2912 (or whitespace open-paren close-paren string-delimiter)) 3001 (or whitespace open-paren close-paren string-delimiter))
2913 (cdr (python-util-comint-last-prompt)) 3002 last-prompt-end
2914 t 1)) 3003 t 1))
2915 (cdr (python-util-comint-last-prompt)) 3004 last-prompt-end
2916 (forward-char (length (match-string-no-properties 0))) 3005 (forward-char (length (match-string-no-properties 0)))
2917 (point)))) 3006 (point))))
2918 (end (point))) 3007 (end (point)))
@@ -2920,13 +3009,12 @@ using that one instead of current buffer's process."
2920 (completion-table-dynamic 3009 (completion-table-dynamic
2921 (apply-partially 3010 (apply-partially
2922 #'python-shell-completion-get-completions 3011 #'python-shell-completion-get-completions
2923 process (buffer-substring-no-properties 3012 process import-statement)))))
2924 (line-beginning-position) end))))))
2925 3013
2926(define-obsolete-function-alias 3014(define-obsolete-function-alias
2927 'python-shell-completion-complete-at-point 3015 'python-shell-completion-complete-at-point
2928 'python-shell-completion-at-point 3016 'python-shell-completion-at-point
2929 "24.5") 3017 "25.1")
2930 3018
2931(defun python-shell-completion-complete-or-indent () 3019(defun python-shell-completion-complete-or-indent ()
2932 "Complete or indent depending on the context. 3020 "Complete or indent depending on the context.
@@ -2935,7 +3023,7 @@ If not try to complete."
2935 (interactive) 3023 (interactive)
2936 (if (string-match "^[[:space:]]*$" 3024 (if (string-match "^[[:space:]]*$"
2937 (buffer-substring (comint-line-beginning-position) 3025 (buffer-substring (comint-line-beginning-position)
2938 (point-marker))) 3026 (point)))
2939 (indent-for-tab-command) 3027 (indent-for-tab-command)
2940 (completion-at-point))) 3028 (completion-at-point)))
2941 3029
@@ -3046,7 +3134,7 @@ inferior Python process is updated properly."
3046(define-obsolete-function-alias 3134(define-obsolete-function-alias
3047 'python-completion-complete-at-point 3135 'python-completion-complete-at-point
3048 'python-completion-at-point 3136 'python-completion-at-point
3049 "24.5") 3137 "25.1")
3050 3138
3051 3139
3052;;; Fill paragraph 3140;;; Fill paragraph
@@ -3266,17 +3354,17 @@ JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3266 (save-restriction 3354 (save-restriction
3267 (narrow-to-region (progn 3355 (narrow-to-region (progn
3268 (while (python-syntax-context 'paren) 3356 (while (python-syntax-context 'paren)
3269 (goto-char (1- (point-marker)))) 3357 (goto-char (1- (point))))
3270 (point-marker)
3271 (line-beginning-position)) 3358 (line-beginning-position))
3272 (progn 3359 (progn
3273 (when (not (python-syntax-context 'paren)) 3360 (when (not (python-syntax-context 'paren))
3274 (end-of-line) 3361 (end-of-line)
3275 (when (not (python-syntax-context 'paren)) 3362 (when (not (python-syntax-context 'paren))
3276 (skip-syntax-backward "^)"))) 3363 (skip-syntax-backward "^)")))
3277 (while (python-syntax-context 'paren) 3364 (while (and (python-syntax-context 'paren)
3278 (goto-char (1+ (point-marker)))) 3365 (not (eobp)))
3279 (point-marker))) 3366 (goto-char (1+ (point))))
3367 (point)))
3280 (let ((paragraph-start "\f\\|[ \t]*$") 3368 (let ((paragraph-start "\f\\|[ \t]*$")
3281 (paragraph-separate ",") 3369 (paragraph-separate ",")
3282 (fill-paragraph-function)) 3370 (fill-paragraph-function))
@@ -3285,7 +3373,8 @@ JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3285 (while (not (eobp)) 3373 (while (not (eobp))
3286 (forward-line 1) 3374 (forward-line 1)
3287 (python-indent-line) 3375 (python-indent-line)
3288 (goto-char (line-end-position)))) t) 3376 (goto-char (line-end-position))))
3377 t)
3289 3378
3290 3379
3291;;; Skeletons 3380;;; Skeletons
@@ -3456,7 +3545,7 @@ The skeleton will be bound to python-skeleton-NAME."
3456(defun python-ffap-module-path (module) 3545(defun python-ffap-module-path (module)
3457 "Function for `ffap-alist' to return path for MODULE." 3546 "Function for `ffap-alist' to return path for MODULE."
3458 (let ((process (or 3547 (let ((process (or
3459 (and (eq major-mode 'inferior-python-mode) 3548 (and (derived-mode-p 'inferior-python-mode)
3460 (get-buffer-process (current-buffer))) 3549 (get-buffer-process (current-buffer)))
3461 (python-shell-get-process)))) 3550 (python-shell-get-process))))
3462 (if (not process) 3551 (if (not process)
@@ -3521,7 +3610,11 @@ See `python-check-command' for the default."
3521 "def __PYDOC_get_help(obj): 3610 "def __PYDOC_get_help(obj):
3522 try: 3611 try:
3523 import inspect 3612 import inspect
3524 if hasattr(obj, 'startswith'): 3613 try:
3614 str_type = basestring
3615 except NameError:
3616 str_type = str
3617 if isinstance(obj, str_type):
3525 obj = eval(obj, globals()) 3618 obj = eval(obj, globals())
3526 doc = inspect.getdoc(obj) 3619 doc = inspect.getdoc(obj)
3527 if not doc and callable(obj): 3620 if not doc and callable(obj):
@@ -3544,10 +3637,7 @@ See `python-check-command' for the default."
3544 doc = doc.splitlines()[0] 3637 doc = doc.splitlines()[0]
3545 except: 3638 except:
3546 doc = '' 3639 doc = ''
3547 try: 3640 print (doc)"
3548 exec('print doc')
3549 except SyntaxError:
3550 print(doc)"
3551 "Python code to setup documentation retrieval." 3641 "Python code to setup documentation retrieval."
3552 :type 'string 3642 :type 'string
3553 :group 'python) 3643 :group 'python)
@@ -3568,9 +3658,12 @@ returns will be used. If not FORCE-PROCESS is passed what
3568 (let ((input (or force-input 3658 (let ((input (or force-input
3569 (python-info-current-symbol t)))) 3659 (python-info-current-symbol t))))
3570 (and input 3660 (and input
3571 (python-shell-send-string-no-output 3661 ;; Prevent resizing the echo area when iPython is
3572 (format python-eldoc-string-code input) 3662 ;; enabled. Bug#18794.
3573 process)))))) 3663 (python-util-strip-string
3664 (python-shell-send-string-no-output
3665 (format python-eldoc-string-code input)
3666 process)))))))
3574 3667
3575(defun python-eldoc-function () 3668(defun python-eldoc-function ()
3576 "`eldoc-documentation-function' for Python. 3669 "`eldoc-documentation-function' for Python.
@@ -4084,7 +4177,7 @@ to \"^python-\"."
4084 (cdr pair)))) 4177 (cdr pair))))
4085 (buffer-local-variables from-buffer))) 4178 (buffer-local-variables from-buffer)))
4086 4179
4087(defvar comint-last-prompt-overlay) ; Shut up, bytecompiler 4180(defvar comint-last-prompt-overlay) ; Shut up, byte compiler.
4088 4181
4089(defun python-util-comint-last-prompt () 4182(defun python-util-comint-last-prompt ()
4090 "Return comint last prompt overlay start and end. 4183 "Return comint last prompt overlay start and end.
@@ -4245,7 +4338,10 @@ Arguments START and END narrow the buffer region to work on."
4245 'python-nav-forward-sexp) 4338 'python-nav-forward-sexp)
4246 4339
4247 (set (make-local-variable 'font-lock-defaults) 4340 (set (make-local-variable 'font-lock-defaults)
4248 '(python-font-lock-keywords nil nil nil nil)) 4341 '(python-font-lock-keywords
4342 nil nil nil nil
4343 (font-lock-syntactic-face-function
4344 . python-font-lock-syntactic-face-function)))
4249 4345
4250 (set (make-local-variable 'syntax-propertize-function) 4346 (set (make-local-variable 'syntax-propertize-function)
4251 python-syntax-propertize-function) 4347 python-syntax-propertize-function)
@@ -4254,8 +4350,9 @@ Arguments START and END narrow the buffer region to work on."
4254 #'python-indent-line-function) 4350 #'python-indent-line-function)
4255 (set (make-local-variable 'indent-region-function) #'python-indent-region) 4351 (set (make-local-variable 'indent-region-function) #'python-indent-region)
4256 ;; Because indentation is not redundant, we cannot safely reindent code. 4352 ;; Because indentation is not redundant, we cannot safely reindent code.
4257 (setq-local electric-indent-inhibit t) 4353 (set (make-local-variable 'electric-indent-inhibit) t)
4258 (setq-local electric-indent-chars (cons ?: electric-indent-chars)) 4354 (set (make-local-variable 'electric-indent-chars)
4355 (cons ?: electric-indent-chars))
4259 4356
4260 ;; Add """ ... """ pairing to electric-pair-mode. 4357 ;; Add """ ... """ pairing to electric-pair-mode.
4261 (add-hook 'post-self-insert-hook 4358 (add-hook 'post-self-insert-hook
@@ -4296,7 +4393,8 @@ Arguments START and END narrow the buffer region to work on."
4296 (add-to-list 'hs-special-modes-alist 4393 (add-to-list 'hs-special-modes-alist
4297 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#" 4394 `(python-mode "^\\s-*\\(?:def\\|class\\)\\>" nil "#"
4298 ,(lambda (_arg) 4395 ,(lambda (_arg)
4299 (python-nav-end-of-defun)) nil)) 4396 (python-nav-end-of-defun))
4397 nil))
4300 4398
4301 (set (make-local-variable 'outline-regexp) 4399 (set (make-local-variable 'outline-regexp)
4302 (python-rx (* space) block-start)) 4400 (python-rx (* space) block-start))