[PATCH 1/5] tuna: Replace bare except clauses with specific exception types

John Kacur <[email protected]> Thu, 11 Jun 2026 16:38:28 -0400
Newsgroups org.kernel.vger.linux-rt-users
Message-ID <[email protected]>
Replace all bare except clauses in tuna-cmd.py with specific exception
types for better error handling and debugging. This improves code quality
by being explicit about what errors are being caught.

Changes:
- inet_diag import (line 75): Use ImportError
- ps_show_sockets (lines 298, 305): Use OSError for process death cases
- irq users lookup (line 357): Use KeyError, ValueError, LookupError
- IRQ parsing (line 418): Use ValueError, KeyError
- socket iteration (line 457): Use OSError, StopIteration
- driver lookup (line 500): Use ValueError
- thread regex (line 571): Use re.error

All existing unit tests pass after these changes.

Assisted-by: Claude:claude-sonnet-4-5
Signed-off-by: John Kacur <[email protected]>
---
 tuna-cmd.py | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/tuna-cmd.py b/tuna-cmd.py
index 6208bd116cea..54f0666fa1bb 100755
--- a/tuna-cmd.py
+++ b/tuna-cmd.py
@@ -72,7 +72,7 @@ def add_handler(loglevel, tofile=False):
 try:
     import inet_diag
     have_inet_diag = True
-except:
+except ImportError:
     have_inet_diag = False
 
 # FIXME: ETOOMANYGLOBALS, we need a class!
@@ -295,14 +295,14 @@ def ps_show_sockets(pid, ps, inodes, inode_re, indent=0):
     dirname = f"/proc/{pid}/fd"
     try:
         filenames = os.listdir(dirname)
-    except:  # Process died
+    except OSError:  # Process died
         return
     sindent = " " * indent
     for filename in filenames:
         pathname = os.path.join(dirname, filename)
         try:
             linkto = os.readlink(pathname)
-        except:  # Process died
+        except OSError:  # Process died
             continue
         inode_match = inode_re.match(linkto)
         if not inode_match:
@@ -354,7 +354,7 @@ def ps_show_thread(pid, affect_children, ps, has_ctxt_switch_info, sock_inodes,
                     users[users.index(u)] = "%s(%s)" % (
                         u, ethtool.get_module(u))
             users = ",".join(users)
-        except:
+        except (KeyError, ValueError, LookupError):
             users = "Not found in /proc/interrupts!"
 
     ctxt_switch_info = ""
@@ -415,7 +415,7 @@ def ps_show(ps, affect_children, thread_list, cpu_list,
                             continue
                     else:
                         in_irq_list = True
-                except:
+                except (ValueError, KeyError):
                     pass
             elif not thread_list:
                 continue
@@ -454,7 +454,7 @@ def load_socktype(socktype, inodes):
     while True:
         try:
             s = idiag.get()
-        except:
+        except (OSError, StopIteration):
             break
         inodes[s.inode()] = s
 
@@ -497,7 +497,7 @@ def find_drivers_by_users(users):
         try:
             idx = u.index('-')
             u = u[:idx]
-        except:
+        except ValueError:
             pass
         if u in nics:
             driver = ethtool.get_module(u)
@@ -568,7 +568,7 @@ def threadstring_to_list(threadstr):
             ps = procfs.pidstats()
             try:
                 thread_list += ps.find_by_regex(re.compile(fnmatch.translate(s)))
-            except:
+            except re.error:
                 thread_list += ps.find_by_name(s)
     return thread_list
 
-- 
2.54.0