3

这可能是有史以来最愚蠢的问题,但我想知道是否有办法在函数(最好是装饰器)周围编写包装器,以便在发生异常时捕获局部变量的内部状态是在该函数中提出。它会在创建本地人时捕获它们,腌制它们,然后在没有引发异常的情况下处理它们,或者如果发现任何异常,则将它们写入文件。

是不是太天马行空了,还是有人玩弄过这种东西?

4

2 回答 2

5

您可以在回溯中捕获f_locals帧上的变量

import sys
import functools

def capturelocals(func):
    @functools.wraps(func)
    def wrapperfunc(*args, **kw):
        try:
            return func(*args, **kw)
        except Exception:
            _, _, tb = sys.exc_info()
            try:
                while tb.tb_next is not None:
                    tb = tb.tb_next  # find innermost frame
                locals = tb.tb_frame.f_locals
                print locals
            finally:
                del tb  # prevent leaking tracebacks
            raise
    return wrapperfunc

为了证明它有效:

>>> @capturelocals
... def foobar():
...     foo = 'bar'
...     spam = 'eggs'
...     raise ValueError('Bam!')
... 
>>> foobar()
{'foo': 'bar', 'spam': 'eggs'}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in wrapperfunc
  File "<stdin>", line 5, in foobar
ValueError: Bam!
于 2013-02-06T00:48:24.317 回答
1

另一种选择是将那些您有兴趣保存的变量作为参数传递给函数,以防发生灾难性事件。

一个可以帮助你做到这一点的装饰器:

import os
try:
    import cPickle as pickle
except ImportError:
    import pickle

def save_args_if_exception(pickle_folder):
    def real_decorator(function):
        def wrapper(*args, **kwargs):
            try:
                function(*args, **kwargs)
            except:
                print 'FAILING SAFE: Saving arguments to folder:'
                print pickle_folder
                for arg_position, arg in enumerate(args, start=1):
                    arg_fname = os.path.join(pickle_folder, str(arg_position) +
                                             '.pickle')
                    print 'Saving positional argument-{}'.format(arg_position)
                    with open(arg_fname, mode='wb') as f:
                        pickle.dump(arg, f)
                print 'Saving keyword arguments (kwargs)'
                with open(os.path.join(pickle_folder, 'kwargs.pickle'),
                          mode='wb') as f:
                    pickle.dump(kwargs, f)
                raise  # re-raise original exception
        return wrapper
    return real_decorator

@save_args_if_exception(os.getcwd())
def important_function(first_arg, second_arg=2, **kwargs):
    print 'About to raise an exception!'
    raise Exception

important_function(3)

请注意,在此示例中,second_arg没有保存到文件中,可能是因为 Python 处理默认参数的方式。根据您的情况,这可能是也可能不是有用的行为。

参考:

于 2013-02-06T01:03:44.140 回答