added printed_exception contextmanager

This commit is contained in:
2021-11-27 15:46:14 +01:00
parent 0c82d8f21b
commit 4c745a6bcf
2 changed files with 15 additions and 2 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ from .channels import load_channels, Channels
from .config import Config
from .elog import Elog
from .eval import arithmetic_eval
from .exceptions import ChainedException, printable_exception
from .exceptions import ChainedException, printable_exception, printed_exception
from .ipy import devices
from .json import json_save, json_load
from .marker import Marker, markers
+14 -1
View File
@@ -1,4 +1,5 @@
from .utils import typename
from contextlib import AbstractContextManager
from .utils import typename, singleton
class ChainedException(Exception):
@@ -22,3 +23,15 @@ def printable_exception(exc):
@singleton
class printed_exception(AbstractContextManager):
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is not None:
name = exc_type.__name__
message = exc_val or ""
print("{}: {}".format(name, message))
return True # this causes the with statement to suppress the exception