From 870247f277db7a2efd842d1f5af6e55af623c2cc Mon Sep 17 00:00:00 2001 From: Jeff Whitlatch Date: Sun, 23 Aug 2026 21:50:19 -0700 Subject: [PATCH] Replace bare except clauses with specific exception types Bare except: catches BaseException, which includes KeyboardInterrupt and SystemExit. This prevents clean Ctrl-C shutdown and can swallow sys.exit() calls. Each clause is narrowed to the specific exception the try block can actually raise. Co-authored-by: Cursor --- meshtastic/mesh_interface.py | 4 ++-- meshtastic/stream_interface.py | 2 +- meshtastic/test.py | 2 +- meshtastic/util.py | 4 ++-- meshtastic/version.py | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/meshtastic/mesh_interface.py b/meshtastic/mesh_interface.py index 1a7dd6eae..24736fcba 100644 --- a/meshtastic/mesh_interface.py +++ b/meshtastic/mesh_interface.py @@ -1348,7 +1348,7 @@ def _handleFromRadio(self, fromRadioBytes): try: newpos = self._fixupPosition(node["position"]) node["position"] = newpos - except: + except (KeyError, TypeError): logger.debug("Node without position") # no longer necessary since we're mutating directly in nodesByNum via _getOrCreateByNum @@ -1516,7 +1516,7 @@ def _nodeNumToId(self, num: int, isDest = True) -> Optional[str]: try: return self.nodesByNum[num]["user"]["id"] # type: ignore[index] - except: + except (KeyError, TypeError): logger.debug(f"Node {num} not found for fromId") return None diff --git a/meshtastic/stream_interface.py b/meshtastic/stream_interface.py index 584079fb6..d62133f3a 100644 --- a/meshtastic/stream_interface.py +++ b/meshtastic/stream_interface.py @@ -161,7 +161,7 @@ def _handleLogByte(self, b): utf = "?" # assume we might fail try: utf = b.decode("utf-8") - except: + except UnicodeDecodeError: pass if utf == "\r": diff --git a/meshtastic/test.py b/meshtastic/test.py index 9c9d62a6b..b863e7480 100644 --- a/meshtastic/test.py +++ b/meshtastic/test.py @@ -205,7 +205,7 @@ def testSimulator() -> None: iface.localNode.exitSimulator() iface.close() logger.info("Integration test successful!") - except: + except Exception: print("Error while testing simulator:", sys.exc_info()[0]) traceback.print_exc() sys.exit(1) diff --git a/meshtastic/util.py b/meshtastic/util.py index 7faa2d6c0..cb11f10ad 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -309,7 +309,7 @@ def _run(self) -> None: try: o = self.queue.get() o() - except: + except Exception: logger.error( f"Unexpected error in deferred execution {sys.exc_info()[0]}" ) @@ -360,7 +360,7 @@ def remove_keys_from_dict(keys: Union[Tuple, List, Set], adict: Dict) -> Dict: for key in keys: try: del adict[key] - except: + except KeyError: pass for val in adict.values(): if isinstance(val, dict): diff --git a/meshtastic/version.py b/meshtastic/version.py index 1856fd560..98d153062 100644 --- a/meshtastic/version.py +++ b/meshtastic/version.py @@ -2,7 +2,7 @@ import sys try: from importlib.metadata import version -except: +except ImportError: import pkg_resources def get_active_version():