Skip to content

Commit 731ab4c

Browse files
committed
fix: replace bare except and try-except-pass with proper exception handling
1 parent 4cf4114 commit 731ab4c

File tree

3 files changed

+63
-63
lines changed

3 files changed

+63
-63
lines changed

examples/demo_timepicker.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,9 @@ def _install_field_debug(self, name, entry_widget, tk_var):
614614
# Ensure the widget can take focus when clicked
615615
try:
616616
entry_widget.configure(takefocus=1)
617-
except Exception: # pylint: disable=broad-except
618-
pass
617+
except Exception as exc: # pylint: disable=broad-except
618+
# Log configuration failure for debugging
619+
self.logger.debug("Failed to configure takefocus for %s: %s", name, exc)
619620

620621
def log_evt(evt_name, event):
621622
try:
@@ -633,8 +634,9 @@ def on_button1(event):
633634
try:
634635
entry_widget.focus_force()
635636
entry_widget.after_idle(entry_widget.focus_force)
636-
except Exception:
637-
pass
637+
except Exception as exc:
638+
# Log focus failure for debugging
639+
self.logger.debug("Failed to force focus for %s: %s", name, exc)
638640
log_evt("Button-1", event)
639641

640642
entry_widget.bind("<Button-1>", on_button1, add="+")
@@ -644,8 +646,9 @@ def on_focus_in(event):
644646
try:
645647
entry_widget.select_range(0, tk.END)
646648
entry_widget.icursor(tk.END)
647-
except Exception:
648-
pass
649+
except Exception as exc:
650+
# Log selection failure for debugging
651+
self.logger.debug("Failed to select range for %s: %s", name, exc)
649652

650653
entry_widget.bind("<FocusIn>", on_focus_in, add="+")
651654

tests/conftest.py

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,23 @@ def root():
5858
if "DISPLAY" not in os.environ:
5959
os.environ["DISPLAY"] = ":0.0"
6060

61-
# Set TCL_LIBRARY environment variable to help with Tcl initialization
61+
# Get the actual Tcl library path from a working Tkinter instance
6262
import sys
63-
if hasattr(sys, 'frozen'):
64-
# Running as compiled executable
65-
tcl_dir = os.path.join(os.path.dirname(sys.executable), 'tcl', 'tcl8.6')
66-
else:
67-
# Running as script
63+
try:
64+
# Create a temporary root to get the actual Tcl library path
65+
temp_check = tk.Tk()
66+
actual_tcl_library = temp_check.tk.eval('info library')
67+
temp_check.destroy()
68+
69+
# Set the environment variable to the actual path
70+
os.environ["TCL_LIBRARY"] = actual_tcl_library
71+
logging.debug(f"Set TCL_LIBRARY to: {actual_tcl_library}")
72+
except Exception as e:
73+
logging.warning(f"Could not determine Tcl library path: {e}")
74+
# Fallback to default paths
6875
tcl_dir = os.path.join(os.path.dirname(sys.executable), 'tcl', 'tcl8.6')
69-
70-
# Try to find Tcl library directory
71-
if os.path.exists(tcl_dir):
72-
os.environ["TCL_LIBRARY"] = tcl_dir
73-
else:
74-
# Try alternative paths
75-
alternative_paths = [
76-
os.path.join(os.path.dirname(sys.executable), 'tcl', 'tcl8.6'),
77-
os.path.join(os.path.dirname(sys.executable), '..', 'tcl', 'tcl8.6'),
78-
os.path.join(os.path.dirname(sys.executable), '..', '..', 'tcl', 'tcl8.6'),
79-
]
80-
for alt_path in alternative_paths:
81-
if os.path.exists(alt_path):
82-
os.environ["TCL_LIBRARY"] = alt_path
83-
break
76+
if os.path.exists(tcl_dir):
77+
os.environ["TCL_LIBRARY"] = tcl_dir
8478

8579
# Create new root window for each test
8680
temp_root = tk.Tk()
@@ -146,8 +140,9 @@ def root():
146140
# Try cleanup for unexpected errors
147141
try:
148142
temp_root.destroy()
149-
except:
150-
pass
143+
except Exception as exc:
144+
# Log cleanup failure for debugging
145+
logging.debug("Failed to destroy temp root during cleanup: %s", exc)
151146
raise
152147

153148

@@ -170,29 +165,23 @@ def root_function():
170165
if "DISPLAY" not in os.environ:
171166
os.environ["DISPLAY"] = ":0.0"
172167

173-
# Set TCL_LIBRARY environment variable to help with Tcl initialization
168+
# Get the actual Tcl library path from a working Tkinter instance
174169
import sys
175-
if hasattr(sys, 'frozen'):
176-
# Running as compiled executable
177-
tcl_dir = os.path.join(os.path.dirname(sys.executable), 'tcl', 'tcl8.6')
178-
else:
179-
# Running as script
170+
try:
171+
# Create a temporary root to get the actual Tcl library path
172+
temp_check = tk.Tk()
173+
actual_tcl_library = temp_check.tk.eval('info library')
174+
temp_check.destroy()
175+
176+
# Set the environment variable to the actual path
177+
os.environ["TCL_LIBRARY"] = actual_tcl_library
178+
logging.debug(f"Set TCL_LIBRARY to: {actual_tcl_library}")
179+
except Exception as e:
180+
logging.warning(f"Could not determine Tcl library path: {e}")
181+
# Fallback to default paths
180182
tcl_dir = os.path.join(os.path.dirname(sys.executable), 'tcl', 'tcl8.6')
181-
182-
# Try to find Tcl library directory
183-
if os.path.exists(tcl_dir):
184-
os.environ["TCL_LIBRARY"] = tcl_dir
185-
else:
186-
# Try alternative paths
187-
alternative_paths = [
188-
os.path.join(os.path.dirname(sys.executable), 'tcl', 'tcl8.6'),
189-
os.path.join(os.path.dirname(sys.executable), '..', 'tcl', 'tcl8.6'),
190-
os.path.join(os.path.dirname(sys.executable), '..', '..', 'tcl', 'tcl8.6'),
191-
]
192-
for alt_path in alternative_paths:
193-
if os.path.exists(alt_path):
194-
os.environ["TCL_LIBRARY"] = alt_path
195-
break
183+
if os.path.exists(tcl_dir):
184+
os.environ["TCL_LIBRARY"] = tcl_dir
196185

197186
# Create new root window
198187
temp_root = tk.Tk()

tests/test_dpi.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import contextlib
99
import ctypes
10+
import logging
1011
import sys
1112
import types
1213
from ctypes import wintypes
@@ -681,8 +682,9 @@ def wrapper(self, *args, **kwargs):
681682
# Create a new root
682683
root = tk.Tk()
683684
root.withdraw()
684-
except:
685+
except Exception as exc:
685686
# If there's any issue, try to create a new root
687+
logging.debug("Failed to use existing root, creating new one: %s", exc)
686688
root = tk.Tk()
687689
root.withdraw()
688690

@@ -4084,9 +4086,10 @@ def test_patch_layout_method_error_handling(self):
40844086
# This should not raise an exception
40854087
try:
40864088
manager._patch_layout_method("test.method", original_method, 2.0)
4087-
except Exception:
4089+
except Exception as exc:
40884090
# If it raises an exception, that's also acceptable for error handling
4089-
pass
4091+
# Log the exception for debugging purposes
4092+
logging.debug("Expected exception in _patch_layout_method: %s", exc)
40904093

40914094
def test_patch_widget_constructor_error_handling(self):
40924095
"""Test _patch_widget_constructor error handling."""
@@ -4101,9 +4104,10 @@ def test_patch_widget_constructor_error_handling(self):
41014104
# This should not raise an exception
41024105
try:
41034106
manager._patch_widget_constructor(mock_widget_class, 2.0, "test.method")
4104-
except Exception:
4107+
except Exception as exc:
41054108
# If it raises an exception, that's also acceptable for error handling
4106-
pass
4109+
# Log the exception for debugging purposes
4110+
logging.debug("Expected exception in _patch_widget_constructor: %s", exc)
41074111

41084112
def test_patch_treeview_method_error_handling(self):
41094113
"""Test _patch_treeview_method error handling."""
@@ -4118,9 +4122,10 @@ def test_patch_treeview_method_error_handling(self):
41184122
# This should not raise an exception
41194123
try:
41204124
manager._patch_treeview_method("test.method", original_method, 2.0, MagicMock())
4121-
except Exception:
4125+
except Exception as exc:
41224126
# If it raises an exception, that's also acceptable for error handling
4123-
pass
4127+
# Log the exception for debugging purposes
4128+
logging.debug("Expected exception in _patch_treeview_method: %s", exc)
41244129

41254130
def test_patch_treeview_method_bound_method_handling(self):
41264131
"""Test _patch_treeview_method with bound method."""
@@ -4140,9 +4145,10 @@ def test_patch_treeview_method_bound_method_handling(self):
41404145
# This should not raise an exception
41414146
try:
41424147
manager._patch_treeview_method("test.method", bound_method, 2.0, MagicMock())
4143-
except Exception:
4148+
except Exception as exc:
41444149
# If it raises an exception, that's also acceptable for error handling
4145-
pass
4150+
# Log the exception for debugging purposes
4151+
logging.debug("Expected exception in _patch_treeview_method (bound): %s", exc)
41464152

41474153
def test_patch_treeview_method_unbound_method_handling(self):
41484154
"""Test _patch_treeview_method with unbound method."""
@@ -4158,9 +4164,10 @@ def test_patch_treeview_method_unbound_method_handling(self):
41584164
# This should not raise an exception
41594165
try:
41604166
manager._patch_treeview_method("ttk.Treeview.column", unbound_method, 2.0, MagicMock())
4161-
except Exception:
4167+
except Exception as exc:
41624168
# If it raises an exception, that's also acceptable for error handling
4163-
pass
4169+
# Log the exception for debugging purposes
4170+
logging.debug("Expected exception in _patch_treeview_method (unbound): %s", exc)
41644171

41654172
def test_patch_treeview_method_style_class_handling(self):
41664173
"""Test _patch_treeview_method with Style class."""
@@ -4176,9 +4183,10 @@ def test_patch_treeview_method_style_class_handling(self):
41764183
# This should not raise an exception
41774184
try:
41784185
manager._patch_treeview_method("ttk.Style.configure", unbound_method, 2.0, MagicMock())
4179-
except Exception:
4186+
except Exception as exc:
41804187
# If it raises an exception, that's also acceptable for error handling
4181-
pass
4188+
# Log the exception for debugging purposes
4189+
logging.debug("Expected exception in _patch_treeview_method (Style): %s", exc)
41824190

41834191
def test_scale_layout_spec_recursive_children_scaling(self):
41844192
"""Test recursive layout spec scaling with children."""

0 commit comments

Comments
 (0)