相关疑难解决方法(0)

5409
推荐指数
22
解决办法
75万
查看次数

Python:在异常之前使用自定义 sys.excepthook 在上下文中的行号处恢复程序

目标是os在下面的代码中解析依赖项并ls执行命令。

我想知道如何用双星做我在下面发表的评论。

我已经知道我可以通过 traceback

也就是说,我想知道是否可以在给定上下文中的给定行号处恢复程序的执行。

import sys

def new_sys_excepthook(type, value, traceback):
    if type == NameError:
        pass
        # Resolution of the exception finding the module dependency and doing the import
        # ** Returning to the line that created the exception with the right
        # ** context and continue the execution with the module 
        # ** dependency resolved 
    # call original excepthook if we can't solve the issue
    sys.__excepthook__(type, value, traceback) 

sys.excepthook = new_sys_excepthook

system("ls")
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
533
查看次数

标签 统计

python ×2

metaclass ×1

oop ×1

python-datamodel ×1