Browse files

Bad handling of exceptions with py-execute-region, lp:1159118

  • Loading branch information...
1 parent 785e3b6 commit f263f5a362e8f22092b905bb0a1c1175291eb482 @andreas-roehler andreas-roehler committed Mar 24, 2013
Showing with 109 additions and 107 deletions.
  1. +82 −80 python-mode.el
  2. +1 −1 test/py-bug-numbered-tests.el
  3. +1 −1 test/python-mode-test.el
  4. +25 −25 test/python-mode-tests.sh
View
162 python-mode.el
@@ -9426,7 +9426,6 @@ When called from a programm, it accepts a string specifying a shell which will b
(defun py-execute-buffer-file (start end pyshellname dedicated switch nostars sepchar split file)
(let* ((oldbuf (current-buffer))
- (py-exception-buffer oldbuf)
(pyshellname (or pyshellname (py-choose-shell)))
(execute-directory
(cond ((ignore-errors (file-name-directory (file-remote-p (buffer-file-name) 'localname))))
@@ -9453,17 +9452,15 @@ When called from a programm, it accepts a string specifying a shell which will b
;; (filebuf (get-buffer file))
(pec (if (string-match "[pP]ython ?3" py-buffer-name)
(format "exec(compile(open('%s').read(), '%s', 'exec')) # PYTHON-MODE\n" localname localname)
- (format "execfile(r'%s') # PYTHON-MODE\n" localname)))
- ;; (comint-scroll-to-bottom-on-output t)
- )
+ (format "execfile(r'%s') # PYTHON-MODE\n" localname))))
(if (file-readable-p file)
(progn
(when (string-match "ipython" (process-name proc))
(sit-for py-ipython-execute-delay))
(setq erg (py-execute-file-base proc file pec procbuf))
(sit-for 0.2)
- (unless (py-postprocess-output-buffer procbuf file)
- (pop-to-buffer py-exception-buffer)
+ (unless (py-postprocess-output-buffer procbuf oldbuf start end file)
+ (pop-to-buffer oldbuf)
(py-shell-manage-windows switch split oldbuf py-buffer-name))
(unless (string= (buffer-name (current-buffer)) (buffer-name procbuf))
(when py-verbose-p (message "Output buffer: %s" procbuf))))
@@ -9505,7 +9502,7 @@ When called from a programm, it accepts a string specifying a shell which will b
(format "execfile(r'%s') # PYTHON-MODE\n" localname)))
(wholebuf (when (boundp 'wholebuf) wholebuf))
(comint-scroll-to-bottom-on-output t)
- erg)
+ erg err-p)
(set-buffer filebuf)
(erase-buffer)
(insert strg)
@@ -9522,41 +9519,40 @@ When called from a programm, it accepts a string specifying a shell which will b
(sit-for py-ipython-execute-delay))
(setq erg (py-execute-file-base proc file pec procbuf))
(sit-for 0.1)
- (if (py-postprocess-output-buffer procbuf file)
- ;; exception occured
- (progn (set-buffer filebuf)
- (switch-to-buffer (current-buffer)))
- (kill-buffer filebuf)
- (py-shell-manage-windows switch split oldbuf py-buffer-name)
- (unless (string= (buffer-name (current-buffer)) (buffer-name procbuf))
- (when py-verbose-p (message "Output buffer: %s" procbuf)))
- (when py-cleanup-temporary
+ (setq err-p (py-postprocess-output-buffer procbuf oldbuf start end file))
+ (py-shell-manage-windows switch split oldbuf py-buffer-name)
+ (unless (string= (buffer-name (current-buffer)) (buffer-name procbuf))
+ (when py-verbose-p (message "Output buffer: %s" procbuf))
+ (when (and (not err-p) py-cleanup-temporary)
(py-delete-temporary file localname filebuf))
(sit-for 0.1)))
(message "%s not readable. %s" file "Do you have write permissions?"))
erg))
+(defun py-execute-python-mode-v5 (start end &optional pyshellname dedicated switch nostars sepchar split file)
+ (let ((py-exception-buffer (current-buffer))
+ (cmd (concat (or pyshellname py-shell-name) (if (string-equal py-which-bufname
+ "Jython")
+ " -"
+ ;; " -c "
+ ""))))
+ (save-excursion
+ (shell-command-on-region start end
+ cmd py-output-buffer))
+ (if (not (get-buffer py-output-buffer))
+ (message "No output.")
+
+ (let ((err-p (py-postprocess-output-buffer py-output-buffer py-exception-buffer start end)))
+ (if err-p
+ (pop-to-buffer py-exception-buffer)
+ (pop-to-buffer py-output-buffer)
+ (goto-char (point-max))
+ (setq erg (copy-marker (point))))))))
+
(defun py-execute-base (start end &optional pyshellname dedicated switch nostars sepchar split file)
"Select the handler. "
- (cond (python-mode-v5-behavior-p
- (let ((py-exception-buffer (current-buffer))
- (cmd (concat (or pyshellname py-shell-name) (if (string-equal py-which-bufname
- "Jython")
- " -"
- ;; " -c "
- ""))))
- (save-excursion
- (shell-command-on-region start end
- cmd py-output-buffer))
- (if (not (get-buffer py-output-buffer))
- (message "No output.")
-
- (let ((err-p (py-postprocess-output-buffer py-output-buffer)))
- (if err-p
- (pop-to-buffer py-exception-buffer)
- (pop-to-buffer py-output-buffer)
- (goto-char (point-max))
- (setq erg (copy-marker (point))))))))
+ (cond (;; enforce proceeding as python-mode.el v5
+ python-mode-v5-behavior-p (py-execute-python-mode-v5 start end pyshellname dedicated switch nostars sepchar split file))
;; No need for a temporary file than
((and (not (buffer-modified-p)) file)
(py-execute-buffer-file start end pyshellname dedicated switch nostars sepchar split file))
@@ -12034,6 +12030,7 @@ Should you need more shells to select, extend this command by adding inside the
(error (concat "Could not detect " py-shell-name " on your sys
tem")))))
+(defalias 'py-toggle-shells 'py-choose-shell)
(defalias 'py-which-shell 'py-choose-shell)
(defun py-choose-shell (&optional arg pyshell dedicated)
"Return an appropriate executable as a string.
@@ -19351,61 +19348,66 @@ Ignores default of `py-switch-buffers-on-execute-p', uses it with value \"non-ni
;;: Subprocess utilities and filters
-(defun py-postprocess-output-buffer (buf &optional file)
+(defun py-postprocess-output-buffer (buf exception-buffer start end &optional file)
"Highlight exceptions found in BUF.
If an exception occurred return t, otherwise return nil. BUF must exist."
(let ((file file)
(expression (concat "^[ ]+File \"\\(" file "\\)\", line \\([0-9]+\\)"))
- line err-p)
- (save-excursion
- (set-buffer buf)
- (when
- (or (and file (re-search-forward expression nil t))
- (and file (re-search-backward expression nil t))
- ;; File "/tmp/python-2246WCK.py", line 7, in <module>
- ;; "^IPython\\|^In \\[[0-9]+\\]: *\\|^>>> \\|^[ ]+File \"\\([^\"]+\\)\", line \\([0-9]+\\)\\|^[^ >]+>[^0-9]+\\([0-9]+\\)"
- (re-search-forward py-traceback-line-re nil t)
- (re-search-backward py-traceback-line-re nil t))
- (or file (setq file (match-string 1)))
- (setq line (if (and (match-string-no-properties 2)
- (save-match-data (string-match "[0-9]" (match-string-no-properties 2))))
- (string-to-number (match-string 2))
- (when (and (match-string-no-properties 3)
- (save-match-data (string-match "[0-9]" (match-string-no-properties 3))))
- (string-to-number (match-string-no-properties 3))))))
- (overlay-put (make-overlay (match-beginning 0) (match-end 0))
- 'face 'highlight))
+ line err-p pattern)
+ (when
+ (or (and file (re-search-forward expression nil t))
+ (and file (re-search-backward expression nil t))
+ ;; File "/tmp/python-2246WCK.py", line 7, in <module>
+ ;; "^IPython\\|^In \\[[0-9]+\\]: *\\|^>>> \\|^[ ]+File \"\\([^\"]+\\)\", line \\([0-9]+\\)\\|^[^ >]+>[^0-9]+\\([0-9]+\\)"
+ (re-search-forward py-traceback-line-re nil t)
+ (re-search-backward py-traceback-line-re nil t))
+ (or file (setq file (match-string 1)))
+ (setq line (if (and (match-string-no-properties 2)
+ (save-match-data (string-match "[0-9]" (match-string-no-properties 2))))
+ (string-to-number (match-string 2))
+ (when (and (match-string-no-properties 3)
+ (save-match-data (string-match "[0-9]" (match-string-no-properties 3))))
+ (string-to-number (match-string-no-properties 3))))))
+ (overlay-put (make-overlay (match-beginning 0) (match-end 0))
+ 'face 'highlight)
+ (setq pattern (progn (forward-line 1)(back-to-indentation)(looking-at ".+")(match-string-no-properties 0)))
+ (goto-char (point-max))
(when (and py-jump-on-exception line)
(beep)
- (when file
- (py-jump-to-exception file line py-line-number-offset))
+ (py-jump-to-exception file line py-line-number-offset exception-buffer pattern start end)
(setq err-p t))
err-p))
-(defun py-jump-to-exception (file line py-line-number-offset)
+(defun py-jump-to-exception (file line py-line-number-offset &optional buffer pattern start end)
"Jump to the Python code in FILE at LINE."
- (let ((buffer (cond ((string-equal file "<stdin>")
- (if (consp py-exception-buffer)
- (cdr py-exception-buffer)
- py-exception-buffer))
- ((and (consp py-exception-buffer)
- (string-equal file (car py-exception-buffer)))
- (cdr py-exception-buffer))
- ((ignore-errors (find-file-noselect file)))
- ;; could not figure out what file the exception
- ;; is pointing to, so prompt for it
- (t (find-file (read-file-name "Exception file: "
- nil
- file t))))))
- ;; Fiddle about with line number
- (setq line (+ py-line-number-offset line))
- (pop-to-buffer buffer)
- ;; Force Python mode
- (unless (eq major-mode 'python-mode)
- (python-mode))
- (goto-char (point-min))
- (forward-line (1- line))
- (message "Jumping to exception in file %s on line %d" file line)))
+ (if buffer
+ (progn
+ (set-buffer buffer)
+ ;; (switch-to-buffer (current-buffer))
+ (goto-char start)
+ (search-forward pattern end nil))
+ (let ((buffer (cond ((string-equal file "<stdin>")
+ (if (consp py-exception-buffer)
+ (cdr py-exception-buffer)
+ py-exception-buffer))
+ ((and (consp py-exception-buffer)
+ (string-equal file (car py-exception-buffer)))
+ (cdr py-exception-buffer))
+ ((ignore-errors (find-file-noselect file)))
+ ;; could not figure out what file the exception
+ ;; is pointing to, so prompt for it
+ (t (find-file (read-file-name "Exception file: "
+ nil
+ file t))))))
+ ;; Fiddle about with line number
+ (setq line (+ py-line-number-offset line))
+ (pop-to-buffer buffer)
+ ;; Force Python mode
+ (unless (eq major-mode 'python-mode)
+ (python-mode))
+ (goto-char (point-min))
+ (forward-line (1- line))
+ (message "Jumping to exception in file %s on line %d" file line))))
(defun py-down-exception (&optional bottom)
"Go to the next line down in the traceback.
View
2 test/py-bug-numbered-tests.el
@@ -5317,7 +5317,7 @@ def foo():
\"\"\"Some docstring.\"\"\"
-__version__ = \"$Revision: 1.3 $\"
+__version__ = \"$Revision: 1.4 $\"
"))
(py-bug-tests-intern 'python-mode-very-slow-lp-1107037-base arg teststring)))
View
2 test/python-mode-test.el
@@ -101,7 +101,6 @@
'py-execute-statement-jython-dedicated-test
'py-execute-statement-jython-dedicated-switch-test
'py-separator-char-test
- 'py-execute-block-python-test
'py-execute-block-python-switch-test
'py-execute-block-python-noswitch-test
'py-execute-block-python-dedicated-test
@@ -423,6 +422,7 @@
'switch-windows-on-execute-p-test
'py-install-directory-path-test
'UnicodeEncodeError-python3-test
+ 'py-execute-block-python-test
))
View
50 test/python-mode-tests.sh
@@ -1115,6 +1115,27 @@ $EMACS -Q --batch --eval "(message (emacs-version))" --eval "(when (featurep 'py
-eval "(assert (boundp 'py-font-lock-keywords) nil \"py-font-lock-keywords not a variable\")" \
-eval "(assert (boundp 'py-font-lock-syntactic-keywords) nil \"py-font-lock-syntactic-keywords not a variable\")" \
-eval "(assert (boundp 'virtualenv-name) nil \"virtualenv-name not a variable\")" \
+--funcall py-execute-block-or-clause-python3-test \
+--funcall py-execute-block-or-clause-python2-test \
+--funcall py-execute-block-or-clause-python2.7-test \
+--funcall py-execute-block-or-clause-python2.7-switch-test \
+--funcall py-execute-block-or-clause-python2.7-noswitch-test \
+--funcall py-execute-block-or-clause-python2.7-dedicated-test \
+--funcall py-execute-block-or-clause-python2.7-dedicated-switch-test \
+--funcall py-execute-partial-expression-test \
+--funcall py-execute-partial-expression-python3-test \
+--funcall py-execute-partial-expression-python2-test \
+--funcall py-execute-partial-expression-python2.7-test \
+est \
+--funcall py-execute-line-python-test \
+--funcall py-execute-line-python3-test \
+--funcall py-execute-line-python2-test \
+--funcall py-execute-line-python2.7-test \
+--funcall py-execute-line-python2.7-switch-test \
+--funcall py-execute-line-python2.7-noswitch-test \
+--funcall py-execute-line-python2.7-dedicated-test \
+--funcall py-execute-line-python2.7-dedicated-switch-test \
+--funcall py-execute-expression-python-test \
--funcall infinite-loop-on-lp-1156426-test \
--funcall py-smart-operator-test \
--funcall python-mode-very-slow-lp-1107037-test \
@@ -1318,34 +1339,10 @@ $EMACS -Q --batch --eval "(message (emacs-version))" --eval "(when (featurep 'py
--funcall py-execute-partial-expression-python-noswitch-test \
--funcall py-execute-partial-expression-python-dedicated-test \
--funcall py-execute-partial-expression-python-dedicated-switch-test \
---funcall py-execute-block-python-test \
--funcall py-execute-block-python3-test \
--funcall py-execute-block-python2-test \
--funcall py-execute-block-python2.7-test \
---funcall py-execute-block-or-clause-python-test \
---funcall py-execute-block-or-clause-python3-test \
---funcall py-execute-block-or-clause-python2-test \
---funcall py-execute-block-or-clause-python2.7-test \
---funcall py-execute-block-or-clause-python2.7-switch-test \
---funcall py-execute-block-or-clause-python2.7-noswitch-test \
---funcall py-execute-block-or-clause-python2.7-dedicated-test \
---funcall py-execute-block-or-clause-python2.7-dedicated-switch-test \
---funcall py-execute-partial-expression-test \
---funcall py-execute-partial-expression-python3-test \
---funcall py-execute-partial-expression-python2-test \
---funcall py-execute-partial-expression-python2.7-test \
-est \
---funcall py-execute-line-python-test \
---funcall py-execute-line-python3-test \
---funcall py-execute-line-python2-test \
---funcall py-execute-line-python2.7-test \
---funcall py-execute-line-python2.7-switch-test \
---funcall py-execute-line-python2.7-noswitch-test \
---funcall py-execute-line-python2.7-dedicated-test \
---funcall py-execute-line-python2.7-dedicated-switch-test \
---funcall py-execute-expression-python-test \
\
---funcall master-file-not-honored-lp:794850-test \
--funcall py-shell-invoking-python3-lp:835151-test \
--funcall py-shell-invoking-python2-lp:835151-test \
--funcall py-shell-invoking-python2.7-lp:835151-test \
@@ -1374,7 +1371,6 @@ est \
--funcall indent-triplequoted-to-itself-lp:752252-test \
--funcall complaint-about-non-ASCII-character-lp-1042949-test \
--funcall py-beginning-of-block-test \
---funcall inconvenient-window-splitting-behavior-python-lp-1018996-test \
--funcall stalls-emacs-probably-due-to-syntax-highlighting-lp-1058261-test \
--funcall tqs-lp:302834-lp:1018994-test \
--funcall py-end-of-def-test \
@@ -1440,3 +1436,7 @@ est \
--funcall indentation-wrong-after-multi-line-parameter-list-lp:871698-test \
--funcall indent-after-inline-comment-lp:873372-test \
--funcall honor-comments-indent-test \
+--funcall py-execute-block-python-test \
+--funcall py-execute-block-or-clause-python-test \
+--funcall master-file-not-honored-lp:794850-test \
+--funcall inconvenient-window-splitting-behavior-python-lp-1018996-test \

0 comments on commit f263f5a

Please sign in to comment.