sys.exc_info 在捕获和引发时不跟踪完整的堆栈跟踪?

Posted

技术标签:

【中文标题】sys.exc_info 在捕获和引发时不跟踪完整的堆栈跟踪?【英文标题】:sys.exc_info not tracing complete stack trace on capture and raise? 【发布时间】:2014-09-12 20:40:12 【问题描述】:

我想捕获并重新引发异常,我已经能够做到这一点。但问题是,reraise后显示的trace没有没有捕获的那么详细。

另一个奇怪的事情是sys.exc_info()[2] 我用于回溯的只是显示外部级别的跟踪,而不是在库内挖掘。而当我使用traceback.format_exc() 时,它只显示库内的跟踪。我想要两条痕迹,如果我不捕获并重新引发,它们实际上会被抛出。

这就是我正在做的:

def raise_location_suggest_error(function):
    def wrap_exceptions(*args, **kwargs):
        try:
            return function(*args, **kwargs)
        except Exception, e:
            # print traceback.format_exc()
            raise (LocationSuggestError(str(e)), None, sys.exc_info()[2])
    return wrap_exceptions

@raise_location_suggest_error
def error_throwing_method():
    # some error thrown

上面的堆栈跟踪是:

Traceback (most recent call last):
  File "/nail/home/prateek/pg/foo-main/foo/web/base_cmd.py", line 453, in run
    return self.execute()
  File "/nail/home/prateek/pg/foo-main/cmds/_request_mapper.py", line 400, in execute
    return action(**match)
  File "/nail/home/prateek/pg/foo-main/cmds/location_suggest.py", line 28, in json_suggestions
    return LocationSuggest(self).json_suggestions()
  File "/nail/home/prateek/pg/foo-main/foo/web/cmd_helpers/location_suggest.py", line 45, in json_suggestions
    _, suggestions = self.get_suggestions()
  File "/nail/home/prateek/pg/foo-main/foo/web/cmd_helpers/location_suggest.py", line 81, in get_suggestions
    .suggest(prefix, country, state, city, recent_locations, exps)
  File "/nail/home/prateek/pg/foo-main/virtualenv_run/lib/python2.6/site-packages/foo_clientlib/decorators/clog_call.py", line 108, in wrapper
    result = method(given_self, *args, **kwargs)
  File "/nail/home/prateek/pg/foo-main/foo/component/search/location_suggest.py", line 113, in suggest
    None, sys.exc_info()[2])

取消注释 print traceback.format_exc() 给出:

File "/nail/home/prateek/pg/foo-main/foo/component/search/location_suggest.py", line 105, in suggest
    AsynchronousHttpClient(headers=self._headers))
  File "/nail/home/prateek/pg/foo-main/virtualenv_run/lib/python2.6/site-packages/swaggerpy/client.py", line 110, in get_client
    return factory(*args, **kwargs).swagger_client
  File "/nail/home/prateek/pg/foo-main/virtualenv_run/lib/python2.6/site-packages/swaggerpy/client.py", line 71, in __call__
    **kwargs)
  File "/nail/home/prateek/pg/foo-main/virtualenv_run/lib/python2.6/site-packages/swaggerpy/client.py", line 79, in build_cached_client
    return CachedClient(SwaggerClient(*args, **kwargs), timeout)
  File "/nail/home/prateek/pg/foo-main/virtualenv_run/lib/python2.6/site-packages/swaggerpy/client.py", line 287, in __init__
    self._api_docs = loader.load_resource_listing(url_or_resource)
  File "/nail/home/prateek/pg/foo-main/virtualenv_run/lib/python2.6/site-packages/swaggerpy/swagger_model.py", line 160, in load_resource_listing
    resource_listing = json_load_url(self.http_client, resources_url)
  File "/nail/home/prateek/pg/foo-main/virtualenv_run/lib/python2.6/site-packages/swaggerpy/swagger_model.py", line 122, in json_load_url
    resp.raise_for_status()
  File "/nail/home/prateek/pg/foo-main/virtualenv_run/lib/python2.6/site-packages/requests/models.py", line 725, in raise_for_status
    raise HTTPError(http_error_msg, response=self)

在删除装饰器时@raise_location_suggest_error 基本上给出了第一个和第二个回溯的串联,这就是我想要的。我可以做些什么来重新提出并获得 full 堆栈跟踪?我正在使用 Python 2.7

【问题讨论】:

【参考方案1】:

从我的角度来看,这是一个愚蠢的错误。 sys.exc_info trace 甚至没有被 raise 作为第三个参数读取。 raise a, b, craise(a, b, c) 不同。如果需要分成两行,请使用\ 而不是括号。更改为 raise a, b, c 解决了我的问题。

【讨论】:

以上是关于sys.exc_info 在捕获和引发时不跟踪完整的堆栈跟踪?的主要内容,如果未能解决你的问题,请参考以下文章

如何将 traceback / sys.exc_info() 值保存在变量中?

捕获异常的两种方式

python 捕捉错误,exception,traceback和sys.exc_info()比较

如果不立即重新引发异常回溯,则隐藏

Python3-笔记-D-001-异常与断言

登录Julia时如何捕获完整的堆栈跟踪信息