OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2017 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import functools | |
6 import logging | |
7 | |
8 | |
9 def Memoize(f): | |
10 """Decorator to cache return values of function.""" | |
11 | |
12 memoize_dict = {} | |
13 @functools.wraps(f) | |
14 def wrapper(*args, **kwargs): | |
15 key = repr((args, kwargs)) | |
16 if key not in memoize_dict: | |
17 try: | |
18 memoize_dict[key] = f(*args, **kwargs) | |
19 except Exception as e: # pylint: disable=broad-except | |
20 memoize_dict[key] = e | |
21 raise | |
22 return_value = memoize_dict[key] | |
23 if isinstance(return_value, Exception): | |
24 raise return_value | |
25 return return_value | |
26 return wrapper | |
27 | |
28 | |
29 def NoRaiseException(default_return_value=None, exception_message=''): | |
mikecase (-- gone --)
2017/02/01 22:13:45
Added exception_message arg instead of always conf
| |
30 """Returns decorator that catches and logs uncaught Exceptions. | |
31 | |
32 Args: | |
33 default_return_value: Value to return in the case of uncaught Exception. | |
34 exception_message: Message for uncaught exceptions. | |
35 """ | |
36 def decorator(f): | |
37 @functools.wraps(f) | |
38 def wrapper(*args, **kwargs): | |
39 try: | |
40 return f(*args, **kwargs) | |
41 except Exception: # pylint: disable=broad-except | |
42 logging.exception(exception_message) | |
43 return default_return_value | |
44 return wrapper | |
45 return decorator | |
OLD | NEW |