python - 使文件名/行号在 Emacs gud 缓冲区中可链接

标签 python emacs pdb gud

我正在通过 gud 缓冲区在我的 Python 测试用例上运行 pdb。当我在我的测试用例中得到堆栈跟踪/失败时,它看起来像这样:

FAIL: test_foo_function (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test/testfoo.py", line 499, in test_foo_function
    self.assertEqual('foo', 'foo')

我希望能够使行像:

File "test/testfoo.py", line 499, in test_foo_function

可点击并转到 testfoo.py 中的第 499 行。

(edit) python-mode 列表中的人员将我带到了 pdbtrack,我能够让它在那里工作。请参阅下面的答案...

最佳答案

感谢 Gerard B 的提示,我明白了。我是从 pdbtrack(shell)而不是纯 pdb 做的,但我相信它应该适用于两者。您需要启用 compilation-shell-minor-mode。并在您的 .emacs 中包含以下代码:

;; if compilation-shell-minor-mode is on, then these regexes
;; will make errors linkable
(defun matt-add-global-compilation-errors (list)
  (dolist (x list)
    (add-to-list 'compilation-error-regexp-alist (car x))
    (setq compilation-error-regexp-alist-alist
      (cons x
            (assq-delete-all (car x)
                             compilation-error-regexp-alist-alist)))))

(matt-add-global-compilation-errors
 `(
   (matt-python ,(concat "^ *File \\(\"?\\)\\([^,\" \n    <>]+\\)\\1"
                    ", lines? \\([0-9]+\\)-?\\([0-9]+\\)?")
           2 (3 . 4) nil 2 2)
   (matt-pdb-stack ,(concat "^>?[[:space:]]*\\(\\([-_./a-zA-Z0-9 ]+\\)"
                       "(\\([0-9]+\\))\\)"
                       "[_a-zA-Z0-9]+()[[:space:]]*->")
              2 3 nil 0 1)
   (matt-python-unittest-err "^  File \"\\([-_./a-zA-Z0-9 ]+\\)\", line \\([0-9]+\\).*" 1 2)
   )
 )

(defun matt-set-local-compilation-errors (errors)
  "Set the buffer local compilation errors.

Ensures than any symbols given are defined in
compilation-error-regexp-alist-alist."
  (dolist (e errors)
     (when (symbolp e)
      (unless (assoc e compilation-error-regexp-alist-alist)
        (error (concat "Error %s is not listed in "
                       "compilation-error-regexp-alist-alist")
               e))))
  (set (make-local-variable 'compilation-error-regexp-alist)
       errors))

然后您可以使用标准编译模式导航来压缩错误堆栈跟踪。

关于python - 使文件名/行号在 Emacs gud 缓冲区中可链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2088307/

相关文章:

python - Tkinter 标签未显示在弹出窗口中

python - 使用opencv和python计算相机到物体的距离

python - 权限被拒绝 pip virtualenv

python - 为什么我的 python 代码在调试器中按预期运行而不是其他方式?

python - 调试 TensorFlow 测试 : pdb or gdb?

python - 语法错误 : invalid syntax to repo init in the AOSP code

在 Mac OS X 上运行 Clojure 的 Emacs (Cocoa Emacs) 与 Aquamacs

emacs - 使用 Emacs (windows) 和 GnuWin32 Grep 失败的 Grep

haskell - 如果 cabal 或工作目录设置为项目目录,Emacs Interactive-Haskell repl 无响应

python - 使用 Python 调试器 (ipdb) 时出现名称错误