python cmd闪退_使用cmd python模块时,如何使程序正常崩溃?
會發生的是,如果您的代碼引發運行時異常并且您的完成不起作用,您就不知道為什么因為沒有打印回溯.嘗試這個非常短的代碼來看看我的意思:程序應該在c = 2“ddda”行崩潰,顯然你要添加一個字符串和一個int,這根本不起作用.但是不是崩潰,而是異常被抓住了,你不知道發生了什么.該程序繼續運行,好像什么也沒發生.
import cmd
class App(cmd.Cmd):
def complete_foo(self,*arg):
# Uncommenting this line will silently crash the progrm
# making it hard to debug.
# Is there a way to force the program to crash ?
c = 2 + "ddda"
return "d dzpo idz dza dpaoi".split(" ")
def do_foo(self,*args):
print "foo"
App().cmdloop()
我的問題是:當有錯誤時如何顯示錯誤? (使用cmd模塊時).
解決方法:
不幸的是,完成者中的異常被捕獲在readline黑暗深處的某個地方.你可以試試這樣的東西:
import cmd
import traceback
def log_exceptions(fun):
def wrapped(*a, **kw):
try:
return fun(*a, **kw)
except Exception:
print traceback.format_exc()
raise
return wrapped
class App(cmd.Cmd):
@log_exceptions
def complete_foo(self,*arg):
# Uncommenting this line will silently crash the progrm
# making it hard to debug.
# Is there a way to force the program to crash ?
c = 2 + "ddda"
return "d dzpo idz dza dpaoi".split(" ")
$python c.py
(Cmd) foo Traceback (most recent call last):
File "c.py", line 7, in wrapped
return fun(*a, **kw)
File "c.py", line 20, in complete_foo
c = 2 + "ddda"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
在調試完成器之后刪除裝飾器,因為從readline內部打印回溯可能會弄亂您的終端.
不,你不能輕易崩潰readline.
標簽:python,cmd
總結
以上是生活随笔為你收集整理的python cmd闪退_使用cmd python模块时,如何使程序正常崩溃?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云电脑账号怎么注册账号(云电脑如何注册账
- 下一篇: python请输入你的名字_实现《你的名