hdl.ir: silence unused elaboratable warning on interpreter crash.
authorwhitequark <whitequark@whitequark.org>
Sun, 26 May 2019 10:42:52 +0000 (10:42 +0000)
committerwhitequark <whitequark@whitequark.org>
Sun, 26 May 2019 10:48:39 +0000 (10:48 +0000)
nmigen/hdl/ir.py

index c462e7e22663318dc91a9e7312bab7f521b19f02..8836b25cc53c87b88b2b6fe61abdb15876c89c36 100644 (file)
@@ -14,6 +14,8 @@ __all__ = ["Elaboratable", "DriverConflict", "Fragment", "Instance"]
 
 
 class Elaboratable(metaclass=ABCMeta):
+    _Elaboratable__silence = False
+
     def __new__(cls, *args, **kwargs):
         self = super().__new__(cls)
         self._Elaboratable__traceback = traceback.extract_stack()[:-1]
@@ -21,6 +23,8 @@ class Elaboratable(metaclass=ABCMeta):
         return self
 
     def __del__(self):
+        if self._Elaboratable__silence:
+            return
         if hasattr(self, "_Elaboratable__used") and not self._Elaboratable__used:
             print("Elaboratable created but never used\n",
                   "Traceback (most recent call last):\n",
@@ -28,6 +32,15 @@ class Elaboratable(metaclass=ABCMeta):
                   file=sys.stderr, sep="")
 
 
+_old_excepthook = sys.excepthook
+def _silence_elaboratable(type, value, traceback):
+    # Don't show anything if the interpreter crashed; that'd just obscure the exception
+    # traceback instead of helping.
+    Elaboratable._Elaboratable__silence = True
+    _old_excepthook(type, value, traceback)
+sys.excepthook = _silence_elaboratable
+
+
 class DriverConflict(UserWarning):
     pass