目的
PythonのTracebackのエラーが表示されるようにしたかった
現状の問題点(2017/02/27)
- 出力の解析が行ごとでしかできない
- 複数行に対する解析で、ループ指定が途中行以降でしかできない(ESLint向けなど)
- => 最終行を指定したキャプチャができない
環境
- Visual Studio Code 1.9.1
解析対象サンプル
def main():
raise Exception("Exception message")
if __name__ == '__main__':
main()
Traceback (most recent call last):
File "c:\path\to\test.py", line 5, in <module>
main()
File "c:\path\to\test.py", line 2, in main
raise Exception("Exception message")
Exception: Exception message
設定例
Python向けtasks.json(拡張子はJSONだけどコメントも書ける)
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "python",
"isShellCommand": true,
"args": [
"${file}"
],
"showOutput": "always",
"problemMatcher": {
"owner": "python",
"fileLocation": "absolute",
"pattern": //[
{
"regexp": "^.*File \"(.*)\", line (\\d+?), in.*$",
"file": 1,
"line": 2
//"loop": true
}
// ホントは最終行のエラーメッセージを取りたいけども
// 現状の"problemMatcher"の構文では対応できない
// https://code.visualstudio.com/Docs/editor/tasks#_defining-a-multiline-problem-matcher
// https://github.com/Microsoft/vscode/blob/master/src/vs/platform/markers/common/problemMatcher.ts
//{
// "regexp": "^(.*):(.*)$",
// "severity": 1,
// "message": 2
//},
//]
}
}
出力例としては以下のようになります。
補足
実行時に例外が投げられた場所で止めたいということであれば、デバッグ画面の左下のブレークポイントで「All Exceptions」にチェックをいれるだけでも解決すると思います。(ただし、F5
を2回押さないといけないので1手順多い)