Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: build/android/pylib/utils/decorators.py

Issue 2664873002: Add logdog_helper script. (Closed)
Patch Set: Add logdog_helper script. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2017 The Chromium Authors. All rights reserved.
jbudorick 2017/02/03 01:35:42 This should have tests.
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
jbudorick 2017/02/03 01:30:30 This is... interesting. I'm not sure we want to do
mikecase (-- gone --) 2017/02/03 18:28:39 Changed to not cache Exceptions.
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=''):
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698