8000 Don't use bare except statements by timhoffm · Pull Request #11995 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Don't use bare except statements #11995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/units/basic_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __call__(self, *args):
if hasattr(a, 'convert_to'):
try:
a = a.convert_to(self.unit)
except:
except Exception:
pass
arg_units.append(a.get_unit())
converted_args.append(a.get_value())
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def hitlist(self, event):
hascursor, info = self.contains(event)
if hascursor:
L.append(self)
except:
except Exception:
import traceback
traceback.print_exc()
print("while checking", self.__class__)
Expand Down Expand Up @@ -1437,7 +1437,7 @@ def properties(self):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
val = func()
except:
except Exception:
continue
else:
d[name[4:]] = val
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ def _update_ticks(self, renderer):
try:
ds1 = self._get_pixel_distance_along_axis(
interval_expanded[0], -0.5)
except:
except Exception:
warnings.warn("Unable to find pixel distance along axis "
"for interval padding of ticks; assuming no "
"interval padding needed.")
Expand All @@ -1087,7 +1087,7 @@ def _update_ticks(self, renderer):
try:
ds2 = self._get_pixel_distance_along_axis(
interval_expanded[1], +0.5)
except:
except Exception:
warnings.warn("Unable to find pixel distance along axis "
"for interval padding of ticks; assuming no "
"interval padding needed.")
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ def _update_enter_leave(self):
try:
if last.inaxes is not None:
last.canvas.callbacks.process('axes_leave_event', last)
except:
except Exception:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure about this one given the comment below it? Raising exceptions in GUI event loops can go very wrong very quickly (aka Qt exits the process!).

Copy link
Member Author
@timhoffm timhoffm Sep 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That try-except block is only there to catch exceptions from the callbacks. KeyboardInterrupt and SystemExit can happen anywhere and Qt has to cope with this somehow. I don't see why we should catch these events if they happen by chance while the canvas callbacks are processed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 That comment almost certainly dates to when anything could be raised as as exception.

pass
# See ticket 2901582.
# I think this is a valid exception to the rule
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def get_width_height_descent(self, text, prop):
# parse metrics from the answer string
try:
width, height, offset = answer.splitlines()[0].split(",")
except:
except Exception:
raise ValueError("Error processing '{}'\nLaTeX Output:\n{}"
.format(text, answer))
w, h, o = float(width[:-2]), float(height[:-2]), float(offset[:-2])
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ def delete_masked_points(*args):
mask = np.isfinite(xd)
if isinstance(mask, np.ndarray):
masks.append(mask)
except: # Fixme: put in tuple of possible exceptions?
except Exception: # Fixme: put in tuple of possible exceptions?
pass
if len(masks):
mask = np.logical_and.reduce(masks)
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -3032,7 +3032,7 @@ def get_type(item, atype=int):
tdict = {None: int, int: float, float: str}
try:
atype(str(item))
except:
except Exception:
return get_type(item, tdict[atype])
return atype

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/testing/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def skip_if_command_unavailable(cmd):
from subprocess import check_output
try:
check_output(cmd)
except:
except Exception:
import pytest
return pytest.mark.skip(reason='missing command: %s' % cmd[0])

Expand Down
4 changes: 2 additions & 2 deletions setupext.py
CA73
Original file line numberDiff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ def do_custom_build(self):
try:
tarball_cache_dir = _get_xdg_cache_dir()
tarball_cache_path = os.path.join(tarball_cache_dir, tarball)
except:
except Exception:
# again, do not really care if this fails
tarball_cache_dir = None
tarball_cache_path = None
Expand Down Expand Up @@ -1640,5 +1640,5 @@ def get_config(cls):
"""
try:
return config.getboolean(cls.config_category, cls.name)
except:
except Exception:
return False # <-- default
2 changes: 1 addition & 1 deletion tools/visualize_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def run(show_browser=True):
try:
import webbrowser
webbrowser.open(index)
except:
except Exception:
show_message = True

if show_message:
Expand Down
0