Skip to content

Commit e7e70ee

Browse files
committed
Avoid bare except
Replaced bare `except:` clauses with `except Exception:`, preventing accidental suppression of system-exit signals (`KeyboardInterrupt`, `SystemExit`) during error handling. Which is almost never intended.
1 parent 55ebdc8 commit e7e70ee

5 files changed

Lines changed: 19 additions & 18 deletions

File tree

CHANGES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
0.4.02, 2026/06/16 -- ALL: Fixed WatchDog.stop() not joining the worker thread. Known to have been causing Xlib race conditions on Linux
1+
0.4.02, 2026/06/16 -- ALL: Replaced bare `except:` clauses with `except Exception:`, preventing accidental suppression of system-exit signals (KeyboardInterrupt, SystemExit) during error handling.
2+
ALL: Fixed WatchDog.stop() not joining the worker thread. Known to have been causing Xlib race conditions on Linux
23
0.4.01, 2024/09/22 -- ALL: Added getAllWindowsDict() general function. Added getPID() method.
34
LINUX: Added bad window filter to check for window.id == 0
45
0.4, 2023/10/11 -- ALL: Added getMonitor() as alias for getDisplay()

src/pywinctl/_main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ def isAlive(self):
591591
"""
592592
try:
593593
alive = bool(self._watchdog and self._watchdog.is_alive())
594-
except:
594+
except Exception:
595595
alive = False
596596
return alive
597597

@@ -663,7 +663,7 @@ def _getInitialValues(self):
663663

664664
if self._changedDisplayCB:
665665
self._display = self._win.getDisplay()
666-
except:
666+
except Exception:
667667
if self._isAliveCB:
668668
self._isAliveCB(False)
669669
self.kill()
@@ -744,7 +744,7 @@ def run(self):
744744
if self._display != display:
745745
self._display = display
746746
self._changedDisplayCB(display)
747-
except:
747+
except Exception:
748748
if self._isAliveCB:
749749
self._isAliveCB(False)
750750
self.kill()

src/pywinctl/_pywinctl_linux.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def __remove_bad_windows(windows: list[str] | list[int] | None) -> list[LinuxWin
369369
# Thanks to Seraphli (https://github.com/Seraphli) for pointing out this issue!
370370
if window:
371371
outList.append(LinuxWindow(window))
372-
except:
372+
except Exception:
373373
pass
374374
return outList
375375

src/pywinctl/_pywinctl_macos.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def getAllWindows() -> list[MacOSWindow]:
133133
try:
134134
pID = item[0]
135135
title = item[1]
136-
except:
136+
except Exception:
137137
continue
138138
for activeApp in activeApps:
139139
if activeApp.processIdentifier() == pID:
@@ -465,10 +465,10 @@ def _getWindowTitles() -> list[list[str]]:
465465
pos = res[1][1][i][j]
466466
size = res[1][2][i][j]
467467
result.append([pID, title, pos, size])
468-
except:
468+
except Exception:
469469
pass
470470
j += 1
471-
except:
471+
except Exception:
472472
pass
473473
return result
474474

@@ -1856,19 +1856,19 @@ def _getaccesskey(self, item_info: dict[str, dict[str, str]] | dict[str, _ItemIn
18561856

18571857
try:
18581858
key = item_info["AXMenuItemCmdChar"]["value"]
1859-
except:
1859+
except Exception:
18601860
key = ""
18611861
try:
18621862
modifiers = int(item_info["AXMenuItemCmdModifiers"]["value"])
1863-
except:
1863+
except Exception:
18641864
modifiers = -1
18651865
try:
18661866
glyph = int(item_info["AXMenuItemCmdGlyph"]["value"])
1867-
except:
1867+
except Exception:
18681868
glyph = -1
18691869
try:
18701870
virtual_key = int(item_info["AXMenuItemCmdVirtualKey"]["value"])
1871-
except:
1871+
except Exception:
18721872
virtual_key = -1
18731873

18741874
modifiers_type = ""

src/pywinctl/_pywinctl_win.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def __remove_bad_windows(windows: list[int] | None):
9393
for window in windows:
9494
try:
9595
outList.append(Win32Window(window))
96-
except:
96+
except Exception:
9797
pass
9898
return outList
9999

@@ -460,7 +460,7 @@ def _getWindowInfo(hWnd: int | str | bytes | bool | None) -> tagWINDOWINFO:
460460
wi.cbSize = ctypes.sizeof(wi)
461461
try:
462462
ctypes.windll.user32.GetWindowInfo(hWnd, ctypes.byref(wi))
463-
except:
463+
except Exception:
464464
pass
465465

466466
# None of these seem to return the right value, at least not in my system, but might be useful for other metrics
@@ -521,7 +521,7 @@ def getExtraFrameSize(self, includeBorder: bool = True) -> tuple[int, int, int,
521521
try:
522522
xBorder = ctypes.windll.user32.GetSystemMetrics(win32con.SM_CXBORDER)
523523
yBorder = ctypes.windll.user32.GetSystemMetrics(win32con.SM_CYBORDER)
524-
except:
524+
except Exception:
525525
xBorder = 1
526526
yBorder = 1
527527
xOffset -= xBorder
@@ -644,7 +644,7 @@ def activate(self, wait: bool = False, user: bool = True) -> bool:
644644
"""
645645
try:
646646
win32gui.SetForegroundWindow(self._hWnd)
647-
except:
647+
except Exception:
648648
pass
649649
return self.isActive
650650

@@ -1019,7 +1019,7 @@ def isAlive(self) -> bool:
10191019
# desc: str = win32api.GetFileVersionInfo(exeName, stringFileInfo) # type: ignore[func-returns-value]
10201020
# if desc:
10211021
# description = desc
1022-
# except:
1022+
# except Exception:
10231023
# pass
10241024
# return description
10251025
#
@@ -1040,7 +1040,7 @@ def isAlive(self) -> bool:
10401040
# w: pywinauto.WindowSpecification = sysTray.child_window(title_re=name, found_index=0)
10411041
# ret: Rect = w.rectangle()
10421042
# return Rect(ret.left, ret.top, ret.right, ret.bottom)
1043-
# except:
1043+
# except Exception:
10441044
# return None
10451045
#
10461046
# def _intToRGBA(color: int) -> Tuple[int, int, int, int]:

0 commit comments

Comments
 (0)