Advertisement
Mr_D3a1h

Managers

Apr 16th, 2023
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. import sys
  2. import traceback
  3. class ExceptionInterceptor:
  4.     def __init__(self, *exceptions):
  5.         self.exceptions = exceptions
  6.  
  7.     def __enter__(self):
  8.         pass
  9.  
  10.     def __exit__(self, exc_type, exc_value, traceback):
  11.         if exc_type in self.exceptions:
  12.             return True
  13.         else:
  14.             return False
  15.  
  16.  
  17.  
  18. class Dumper:
  19.     def __init__(self, stream):
  20.         self.stream = stream
  21.  
  22.     def __enter__(self):
  23.         return self
  24.  
  25.     def __exit__(self, exc_type, exc_val, exc_tb):
  26.         if exc_type is not None:
  27.             self.stream.write(f"{exc_val}\\n")
  28.             self.stream.write(''.join(traceback.format_tb(exc_tb)))
  29. class retyper:
  30.     def __init__(self, type_from, type_to):
  31.         self.type_from = type_from
  32.         self.type_to = type_to
  33.  
  34.     def __enter__(self):
  35.         pass
  36.  
  37.     def __exit__(self, exc_type, exc_value, traceback):
  38.         if exc_type == self.type_from:
  39.             raise self.type_to(*exc_value.args) from exc_value
  40. def foo():
  41.     i = 1 / 0
  42.  
  43.  
  44. with Dumper(sys.stderr):
  45.     foo()
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement