mat*_*son 11 python emacs gud pdb
我正在通过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')
Run Code Online (Sandbox Code Playgroud)
我希望能够像以下一样制作线条:
File "test/testfoo.py", line 499, in test_foo_function
Run Code Online (Sandbox Code Playgroud)
可点击并转到testfoo.py中的第499行.
(编辑)python模式列表上的人们带我到pdbtrack,我能够让它在那里工作.见下面的答案......
多亏了 Gerard BI 的提示,它才弄清楚。我是从 pdbtrack (shell) 而不是纯 pdb 做这个的,但我相信它应该适用于两者。您需要启用compile-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))
Run Code Online (Sandbox Code Playgroud)
然后您可以使用标准编译模式导航来压缩错误堆栈跟踪。
我认为您想要自定义的是compilation-parse-errors-filename-function,它是一个函数,它接受文件名,并返回要显示的文件名的修改版本。这是一个缓冲区局部变量,因此您应该在每个将显示 python 错误的缓冲区中设置它(可能有一个合适的钩子可以使用,我没有安装 python 模式,所以我无法查找它)。您可以用来propertize返回输入文件名的版本,该版本充当加载实际文件的超链接。propertytize 在 elisp 手册中有详细记录。
如果compilation-parse-errors-filename-function没有被调用,那么您需要添加一个列表compilation-error-regexp-alist-alist(确实是alist-alist,这不是拼写错误),它是模式名称的列表,后跟要匹配的正则表达式错误,以及错误正则表达式匹配中的匹配行号、文件名等信息的数字索引。