"断言"烦恼 [英] "assert" annoyance

查看:56
本文介绍了"断言"烦恼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在我的代码中有一些断言声明来验证没有

一些不可能的声明条件。它们在调试和

课程中非常有用我将它们留在了真实的位置。该计划的运行。 Umpteen

小时跑步,断言失败,当然因为失败

是不可能,我没有抓住异常所以整个程序

崩溃了。不管怎么说,我不知道我做了什么,

,因为它必须在外部范围被抓住,其中

数据我关心不再是在附近,否则我将不得不提前预测我需要检查的内容并将其作为

向断言传递声明。


我真正想要的是任何断言失败,在

程序中的任何地方,陷入调试器而不会超出范围

失败发生的地方,所以我可以检查本地帧。

看起来很自然,但我没有看到明显的方法。我错过了什么?b $ b错过了什么?我想我可以用启动pdb的

函数调用替换所有断言,但为什么还要打断一个断言

语句?

So I have some assert statements in my code to verify the absence of
some "impossible" conditions. They were useful in debugging and of
course I left them in place for "real" runs of the program. Umpteen
hours into a run, an assertion failed, and of course since failure
was "impossible", I didn''t catch the exception so the whole program
crashed. I don''t know what I''d have done with the exception anyway,
since it would have had to be caught at an outer scope where the
data I cared about was no longer around, or else I''d have had to
predict in advance what I needed to examine and pass that as a
an arg to the assert statement.

What I really want is for any assertion failure, anywhere in the
program, to trap to the debugger WITHOUT blowing out of the scope
where the failure happened, so I can examine the local frame. That
just seems natural, but I don''t see an obvious way to do it. Am I
missing something? I guess I could replace all the assertions with
function calls that launch pdb, but why bother having an assert
statement?

推荐答案

6月22日凌晨1点31分,Paul Rubin< http://phr...@NOSPAM.invalidwrote:
On Jun 22, 1:31 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:

我真正想要的是任何断言失败,在

程序中的任何位置,陷入调试器而不会超出范围

失败发生的地方,所以我可以检查本地框架。

看起来很自然,但我没有看到明显的方法。
What I really want is for any assertion failure, anywhere in the
program, to trap to the debugger WITHOUT blowing out of the scope
where the failure happened, so I can examine the local frame. That
just seems natural, but I don''t see an obvious way to do it.



你可以通过pdb运行整个程序:

----

#!/ usr / bin / env python -m pdb


print" Hello!"

断言错误

print" ...世界!"

----

You could run the entire program through pdb:
----
#!/usr/bin/env python -m pdb

print "Hello!"
assert False
print "...world!"
----


2007年6月21日,Miles< se **** *****@gmail.com写道:
On 6/21/07, Miles <se*********@gmail.comwrote:

6月22日凌晨1点31分,Paul Rubin< http://phr...@NOSPAM。 invalidwrote:
On Jun 22, 1:31 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:

我真正想要的是任何断言失败,在

程序中的任何地方,陷阱到调试器而不会吹出范围

故障发生的地方,所以我可以检查本地框架。

看起来很自然,但我没有看到明显的方法。
What I really want is for any assertion failure, anywhere in the
program, to trap to the debugger WITHOUT blowing out of the scope
where the failure happened, so I can examine the local frame. That
just seems natural, but I don''t see an obvious way to do it.



你可以通过pdb运行整个程序:

----

#!/ usr / bin / env python -m pdb


print" Hello!"

断言错误

print" ... world!"

----


You could run the entire program through pdb:
----
#!/usr/bin/env python -m pdb

print "Hello!"
assert False
print "...world!"
----



你只能将一个参数传递给你用
调用的命令
shebang序列,所以这不会像你写的那样工作。


-

Evan Klitzke< ev ** @ yelp.com>

You can only pass one argument to a command that you invoke with the
shebang sequence, so this won''t work the way you wrote it.

--
Evan Klitzke <ev**@yelp.com>


6月22日凌晨2:45,Evan Klitzke < e ... @ yelp.comwrote:
On Jun 22, 2:45 am, "Evan Klitzke" <e...@yelp.comwrote:

2007年6月21日,Miles< semantic ... @ gmail.comwrote:
On 6/21/07, Miles <semantic...@gmail.comwrote:

6月22日凌晨1点31分,Paul Rubin< http://phr...@NOSPAM.invalidwrote:
On Jun 22, 1:31 am, Paul Rubin <http://phr...@NOSPAM.invalidwrote:

我真正想要的是任何断言失败,在

程序中的任何位置,陷入调试器而不会超出范围

发生故障的地方,所以我可以检查本地框架。

看起来很自然,但我没有看到明显的方法。
What I really want is for any assertion failure, anywhere in the
program, to trap to the debugger WITHOUT blowing out of the scope
where the failure happened, so I can examine the local frame. That
just seems natural, but I don''t see an obvious way to do it.


你可以通过pdb运行整个程序:

----

#!/ usr / bin / env python -m pdb
You could run the entire program through pdb:
----
#!/usr/bin/env python -m pdb


print" Hello!"

断言错误

print" ... world!"

----
print "Hello!"
assert False
print "...world!"
----



你可以只将一个参数传递给你用

shebang序列调用的命令,所以这不会像你写的那样工作。


-

Evan Klitzke< e ... @ yelp.com>


You can only pass one argument to a command that you invoke with the
shebang sequence, so this won''t work the way you wrote it.

--
Evan Klitzke <e...@yelp.com>



它实际上可以在我的系统上运行(OS X);我没有意识到这不是b
便携式。

It actually does work on my system (OS X); I didn''t realize it wasn''t
portable.


这篇关于&QUOT;断言&QUOT;烦恼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆