🖥

#python #djago の pdb で pdb.set_trace() 的なデバッグをしたいけど 標準入出力の関係でうまく使えない場合

2019/05/08に公開

ファイルから標準入力を与えてスクリプト実行する場合など

うまくゆかない。

$ python < eg.py

--Return--
> <stdin>(8)<module>()->None
(Pdb)
Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "/Users/yumainaura/.pyenv/versions/3.7.3/lib/python3.7/bdb.py", line 92, in trace_dispatch
    return self.dispatch_return(frame, arg)
  File "/Users/yumainaura/.pyenv/versions/3.7.3/lib/python3.7/bdb.py", line 154, in dispatch_return
    if self.quitting: raise BdbQuit
bdb.BdbQuit

python なら

コマンドオプションでファイル指定しよう。

$ python -m pdb eg.py

django shell なら

標準入力を吸収してから tty とつなげてやれば良さそう。

import pdb

lines = sys.stdin.readlines()
sys.stdin = open('/dev/tty')

pdb.set_trace()

ref

python - Use pdb.set_trace() in a script that reads stdin via a pipe - Stack Overflow

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/1710

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-05-08

Discussion