borgend/exprotect.py

Wed, 07 Feb 2018 20:39:01 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Wed, 07 Feb 2018 20:39:01 +0000
changeset 113
6993964140bd
parent 106
a7bdc239ef62
permissions
-rw-r--r--

Time snapshot fixes.
Python's default arguments are purely idiotic (aka. pythonic): generated
only once. This makes sense in a purely functional language, which Python
lightyears away from, but severely limits their usefulness in an imperative
language. Decorators also seem clumsy for this, as one would have to tell
the number of positional arguments for things to work nice, being able to
pass the snapshot both positionally and as keyword. No luck.
So have to do things the old-fashioned hard way.

106
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 #
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Borgend by Tuomo Valkonen, 2018
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 #
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4 # Except-catching decocators to catch bugs in callbacks
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 #
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 import logging
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 logger=logging.getLogger(__name__)
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 def protect_noreturn(fn):
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 name=fn.__name__
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 def wrapper(*args, **kwargs):
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 try:
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 fn(*args, **kwargs)
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 except:
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 logger.exception('bug in ' + name)
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 return wrapper
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21 def protect_return(default):
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 def wrap(fn):
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 name=fn.__name__
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24 def wrapper(*args, **kwargs):
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 try:
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 return fn(*args, **kwargs)
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 except:
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 logger.exception('bug in ' + name)
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 return default
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30 return wrapper
a7bdc239ef62 Added exeption protection decorators to callbacks.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 return wrap

mercurial