使用getattr()跟踪回溯

时间:2015-03-31 16:27:05

标签: python

我通过getattr(module,method_name)调用它的方法 并且调用这些方法不会在traceback中显示,因此很难调试 如何调用这些方法显示在回溯中?

回溯:

File "/home/master/venvs/hello_traversal/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/master/djcode/django_traversal_helloworld/traversal/router.py" in route_factory
  117.     return router.resource_tree_traverse()
File "/home/master/djcode/django_traversal_helloworld/traversal/router.py" in resource_tree_traverse
  112.         return self.get_view(view_name, params)
File "/home/master/djcode/django_traversal_helloworld/traversal/router.py" in get_view
  74.                 TFactory = self.get_path(t_factory_path)
File "/home/master/djcode/django_traversal_helloworld/traversal/router.py" in get_path
  45.             m = importlib.import_module(module)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)

但我打电话给'post_list_view'方法,它不在追溯

1 个答案:

答案 0 :(得分:0)

我通过添加以下代码来解决这个问题:

     try:
          exc_info = sys.exc_info()
          ...
     finally:
        # Display the *original* exception in traceback  exclude KeyError
        if exc_info[0]:
            if not isinstance(exc_info[1], KeyError):
                traceback.print_exception(*exc_info)
        del exc_info