39 lines
1.5 KiB
Python
39 lines
1.5 KiB
Python
"""Retain exceptions from StorageNode calls for the Rust test runner."""
|
|
from functools import wraps
|
|
from inspect import getattr_static
|
|
|
|
|
|
def watch(cls):
|
|
if not isinstance(cls, type):
|
|
raise TypeError("StorageNode must be a class")
|
|
missing = sorted(getattr(cls, "__abstractmethods__", ()))
|
|
if missing:
|
|
raise TypeError(f"{cls.__name__} must implement {', '.join(missing)}")
|
|
errors = []
|
|
|
|
def checked(name, method):
|
|
@wraps(method)
|
|
def call(*args, **kwargs):
|
|
try:
|
|
result = method(*args, **kwargs)
|
|
except BaseException as exc:
|
|
errors[:] = [f"{name}: {type(exc).__name__}: {exc}"]
|
|
raise
|
|
if errors:
|
|
errors.clear() # A nested exception was handled by the solution.
|
|
return result
|
|
|
|
return call
|
|
|
|
for name in ("__new__", "__init__", "on_start", "on_local_message", "on_message", "on_timer",
|
|
"get_state", "set_state", "__deepcopy__"):
|
|
# Preserve static and class methods and inherited implementations.
|
|
method = getattr_static(cls, name, None)
|
|
if name == "__new__" and method is object.__new__:
|
|
continue # Keep the default allocation and constructor argument handling.
|
|
if isinstance(method, (staticmethod, classmethod)):
|
|
setattr(cls, name, type(method)(checked(name, method.__func__)))
|
|
elif callable(method):
|
|
setattr(cls, name, checked(name, method))
|
|
return errors
|