您可以使用
sys.excepthook和通过猴子补丁来解决此问题
sys.exit():
import atexitimport sysclass ExitHooks(object): def __init__(self): self.exit_pre = None self.exception = None def hook(self): self._orig_exit = sys.exit sys.exit = self.exit sys.excepthook = self.exc_handler def exit(self, pre=0): self.exit_pre = pre self._orig_exit(pre) def exc_handler(self, exc_type, exc, *args): self.exception = exchooks = ExitHooks()hooks.hook()def foo(): if hooks.exit_pre is not None: print("death by sys.exit(%d)" % hooks.exit_pre) elif hooks.exception is not None: print("death by exception: %s" % hooks.exception) else: print("natural death")atexit.register(foo)# testsys.exit(1)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)