emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: python.el, shell-send-region and exception handling


From: Yuri D'Elia
Subject: Re: python.el, shell-send-region and exception handling
Date: Thu, 25 Jun 2015 15:55:32 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.0

On 06/23/2015 03:42 PM, Yuri D'Elia wrote:
> On 01/18/2015 02:31 AM, Fabián Ezequiel Gallina wrote:
>>> If I wanted to implement such a feature, how would you suggest to
>>> implement it?
>>
>> A simple way to achieve this would be to create a comint output filter
>> function.  The `python-pdbtrack-comint-output-filter-function` is a nice
>> guide to start.  You would check with a regexp for the occurrence of an
>> exception and act accordingly.
>>
>> I plan to add this to python.el.  If you'd like your implementation to
>> be considered feel free to propose it, otherwise expect my approach to
>> land in the next few weeks.
> 
> Hi again Fabián, did you eventually develop something in that regard?

So here's a crude approach:

(defun python-shell-show-exception (buffer)
  (pop-to-buffer buffer))

(defun python-comint-show-exceptions (output)
  (save-excursion
    (when (re-search-backward "\\bTraceback (most recent call last):\n  File "
                              comint-last-output-start t)
      (python-shell-show-exception (current-buffer)))))

(add-hook 'inferior-python-mode-hook
          (lambda ()
            (add-hook 'comint-output-filter-functions
                      'python-comint-show-exceptions)))

This will call `python-shell-show-exception' (which is just a simple stub here)
if a traceback is detected in the output buffer.

Matching on the regex is pretty crude, but I guess there's nothing much
better that can be done? You cannot assume it's the last output, and not the
first either, so the regex just tries to match the beginning of a traceback.

At first, it seems to work as intended. But clearly some better names/
customization is needed in order to let the user decide what to do in
case of exceptions.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]