我如何知道方法调用可能会抛出哪些异常? [英] How can I know which exceptions might be thrown from a method call?

查看:34
本文介绍了我如何知道方法调用可能会抛出哪些异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法(在编码时)知道在执行 python 代码时会出现哪些异常?

我最终在 90% 的情况下捕获了基本异常类,因为我不知道可能会抛出哪种异常类型(阅读文档并不总是有帮助,因为很多时候异常可以从深层传播.并且很多时候文档没有更新或正确).

是否有某种工具可以检查这一点(例如通过阅读 Python 代码和库)?

解决方案

我想一个解决方案可能只是因为缺乏静态类型规则而不够精确.

我不知道有什么工具可以检查异常,但是您可以想出符合您需求的自己的工具(这是玩静态分析的好机会).

作为第一次尝试,您可以编写一个函数来构建 AST,找到所有 Raise 节点,然后尝试找出引发异常的常见模式(例如直接调用构造函数)

x 成为以下程序:

x = '''如果 f(x):引发 IOError(errno.ENOENT,'未找到')别的:e = g(x)提高e'''

使用 compiler 包构建 AST:

tree = compiler.parse(x)

然后定义一个Raise访问者类:

class RaiseVisitor(object):def __init__(self):self.nodes = []def visitRaise(self, n):self.nodes.append(n)

并走AST收集Raise节点:

v = RaiseVisitor()compiler.walk(tree, v)>>>打印 v.nodes[增加(调用函数(名称('IOError'),[Getattr(Name('errno'), 'ENOENT'), Const('not found')],无,无),无,无),提高(名称('e'),无,无),]

您可以继续使用编译器符号表解析符号,分析数据依赖性等.或者您可以推断出,CallFunc(Name('IOError'), ...)肯定应该意味着提高 IOError",这对于快速的实际结果来说是可以的 :)

Is there a way knowing (at coding time) which exceptions to expect when executing python code?

I end up catching the base Exception class 90% of the time since I don't know which exception type might be thrown (reading the documentation doesn't always help, since many times an exception can be propagated from the deep. And many times the documentation is not updated or correct).

Is there some kind of tool to check this (like by reading the Python code and libs)?

解决方案

I guess a solution could be only imprecise because of lack of static typing rules.

I'm not aware of some tool that checks exceptions, but you could come up with your own tool matching your needs (a good chance to play a little with static analysis).

As a first attempt, you could write a function that builds an AST, finds all Raise nodes, and then tries to figure out common patterns of raising exceptions (e. g. calling a constructor directly)

Let x be the following program:

x = '''
if f(x):
    raise IOError(errno.ENOENT, 'not found')
else:
    e = g(x)
    raise e
'''

Build the AST using the compiler package:

tree = compiler.parse(x)

Then define a Raise visitor class:

class RaiseVisitor(object):
    def __init__(self):
        self.nodes = []
    def visitRaise(self, n):
        self.nodes.append(n)

And walk the AST collecting Raise nodes:

v = RaiseVisitor()
compiler.walk(tree, v)

>>> print v.nodes
[
    Raise(
        CallFunc(
            Name('IOError'),
            [Getattr(Name('errno'), 'ENOENT'), Const('not found')],
            None, None),
        None, None),
    Raise(Name('e'), None, None),
]

You may continue by resolving symbols using compiler symbol tables, analyzing data dependencies, etc. Or you may just deduce, that CallFunc(Name('IOError'), ...) "should definitely mean raising IOError", which is quite OK for quick practical results :)

这篇关于我如何知道方法调用可能会抛出哪些异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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