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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值