Skip to content

Commit

Permalink
FIX: Avoid use of bare except
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner committed Aug 12, 2018
1 parent 204f2c8 commit 0aca113
Show file tree
Hide file tree
Showing 31 changed files with 90 additions and 158 deletions.
2 changes: 1 addition & 1 deletion benchmarks/benchmarks/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ def track_all(self, name, ret_value, solver):
return av_results[solver]['mean_nfev']
else:
raise ValueError()
except:
except Exception:
print("".join(traceback.format_exc()))
self.results[name] = "".join(traceback.format_exc())

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benchmarks/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def setup(self, fromfmt, tofmt):

try:
self.fn = getattr(base, 'to' + tofmt)
except:
except Exception:
def fn():
raise RuntimeError()
self.fn = fn
Expand Down
8 changes: 4 additions & 4 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,24 +374,24 @@ def linkcode_resolve(domain, info):
for part in fullname.split('.'):
try:
obj = getattr(obj, part)
except:
except Exception:
return None

try:
fn = inspect.getsourcefile(obj)
except:
except Exception:
fn = None
if not fn:
try:
fn = inspect.getsourcefile(sys.modules[obj.__module__])
except:
except Exception:
fn = None
if not fn:
return None

try:
source, lineno = inspect.getsourcelines(obj)
except:
except Exception:
lineno = None

if lineno:
Expand Down
2 changes: 1 addition & 1 deletion pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def _build_mpkg(pyver):
def dmg():
try:
pyver = options.dmg.python_version
except:
except Exception:
pyver = PYVER
idirs = options.installers.installersdir

Expand Down
2 changes: 1 addition & 1 deletion runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def build_project(args):
last_log_size = log_size

ret = p.wait()
except:
except: # noqa: E722
p.terminate()
raise

Expand Down
2 changes: 1 addition & 1 deletion scipy/_lib/_ccallback_c.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ cdef double test_thunk_cython(double a, int *error_flag, void *data) nogil excep
with gil:
try:
return float((<object>callback.py_function)(a))
except:
except: # noqa: E722
error_flag[0] = 1
raise

Expand Down
2 changes: 1 addition & 1 deletion scipy/_lib/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def make(self, src_templ, evaldict=None, addsource=False, **attrs):
try:
code = compile(src, filename, 'single')
exec(code, evaldict)
except:
except: # noqa: E722
print('Error in generated code:', file=sys.stderr)
print(src, file=sys.stderr)
raise
Expand Down
2 changes: 1 addition & 1 deletion scipy/_lib/tests/test__threadsafety.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def worker(k):
time.sleep(0.1 * k)
assert_(failflag[0])
failflag[0] = False
except:
except Exception:
exceptions_raised.append(traceback.format_exc(2))

threads = [threading.Thread(target=lambda k=k: worker(k))
Expand Down
6 changes: 3 additions & 3 deletions scipy/cluster/tests/test_hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
# before importing pyplot
import matplotlib.pyplot as plt
have_matplotlib = True
except:
except Exception:
have_matplotlib = False


Expand Down Expand Up @@ -327,11 +327,11 @@ def test_is_isomorphic_6(self):
# nonisomorphic.)
for nc in [2, 3, 5]:
self.help_is_isomorphic_randperm(1000, nc, True, 5)

def test_is_isomorphic_7(self):
# Regression test for gh-6271
assert_(not is_isomorphic([1, 2, 3], [1, 1, 1]))

def help_is_isomorphic_randperm(self, nobs, nclusters, noniso=False, nerrors=0):
for k in range(3):
a = np.int_(np.random.rand(nobs) * nclusters)
Expand Down
12 changes: 6 additions & 6 deletions scipy/integrate/quadrature.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,13 @@ def romb(y, dx=1.0, axis=-1, show=False):
-0.742561336672229
>>> integrate.romb(y, show=True)
Richardson Extrapolation Table for Romberg Integration
Richardson Extrapolation Table for Romberg Integration
====================================================================
-0.81576
4.63862 6.45674
-1.10581 -3.02062 -3.65245
-2.57379 -3.06311 -3.06595 -3.05664
-1.34093 -0.92997 -0.78776 -0.75160 -0.74256
-0.81576
4.63862 6.45674
-1.10581 -3.02062 -3.65245
-2.57379 -3.06311 -3.06595 -3.05664
-1.34093 -0.92997 -0.78776 -0.75160 -0.74256
====================================================================
-0.742561336672229
"""
Expand Down
12 changes: 4 additions & 8 deletions scipy/interpolate/tests/test_bsplines.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,8 @@ def test_tck(self):
assert_equal(b.k, tck[2])

# b.tck is read-only
try:
with pytest.raises(AttributeError):
b.tck = 'foo'
except AttributeError:
pass
except:
raise AssertionError("AttributeError not raised.")

def test_degree_0(self):
xx = np.linspace(0, 1, 10)
Expand Down Expand Up @@ -705,7 +701,7 @@ def test_sproot(self):
def test_splint(self):
# test that splint accepts BSpline objects
b, b2 = self.b, self.b2
assert_allclose(splint(0, 1, b),
assert_allclose(splint(0, 1, b),
splint(0, 1, b.tck), atol=1e-14)
assert_allclose(splint(0, 1, b),
b.integrate(0, 1), atol=1e-14)
Expand Down Expand Up @@ -874,13 +870,13 @@ def test_knots_not_data_sites(self):
atol=1e-14)

def test_minimum_points_and_deriv(self):
# interpolation of f(x) = x**3 between 0 and 1. f'(x) = 3 * xx**2 and
# interpolation of f(x) = x**3 between 0 and 1. f'(x) = 3 * xx**2 and
# f'(0) = 0, f'(1) = 3.
k = 3
x = [0., 1.]
y = [0., 1.]
b = make_interp_spline(x, y, k, bc_type=([(1, 0.)], [(1, 3.)]))

xx = np.linspace(0., 1.)
yy = xx**3
assert_allclose(b(xx), yy, atol=1e-14, rtol=1e-14)
Expand Down
21 changes: 7 additions & 14 deletions scipy/interpolate/tests/test_interpnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from numpy.testing import assert_equal, assert_allclose, assert_almost_equal
from pytest import raises as assert_raises
import pytest
from scipy._lib._numpy_compat import suppress_warnings

import scipy.interpolate.interpnd as interpnd
Expand Down Expand Up @@ -151,14 +152,10 @@ def test_tri_input_rescale(self):
y = y - 3j*y

tri = qhull.Delaunay(x)
try:
match = ("Rescaling is not supported when passing a "
"Delaunay triangulation as ``points``.")
with pytest.raises(ValueError, match=match):
interpnd.LinearNDInterpolator(tri, y, rescale=True)(x)
except ValueError as e:
if str(e) != ("Rescaling is not supported when passing a "
"Delaunay triangulation as ``points``."):
raise
except:
raise

def test_pickle(self):
# Test at single points
Expand Down Expand Up @@ -298,14 +295,10 @@ def test_tri_input_rescale(self):
y = y - 3j*y

tri = qhull.Delaunay(x)
try:
match = ("Rescaling is not supported when passing a "
"Delaunay triangulation as ``points``.")
with pytest.raises(ValueError, match=match):
interpnd.CloughTocher2DInterpolator(tri, y, rescale=True)(x)
except ValueError as a:
if str(a) != ("Rescaling is not supported when passing a "
"Delaunay triangulation as ``points``."):
raise
except:
raise

def test_tripoints_input_rescale(self):
# Test at single points
Expand Down
9 changes: 2 additions & 7 deletions scipy/interpolate/tests/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,14 +778,9 @@ def test_extend(self):
x = np.arange(0., 11.)
y = np.array([0., 2., 1., 3., 2., 6., 5.5, 5.5, 2.7, 5.1, 3.])
ak = Akima1DInterpolator(x, y)
try:
match = "Extending a 1D Akima interpolator is not yet implemented"
with pytest.raises(NotImplementedError, match=match):
ak.extend(None, None)
except NotImplementedError as e:
if str(e) != ("Extending a 1D Akima interpolator is not "
"yet implemented"):
raise
except:
raise


class TestPPolyCommon(object):
Expand Down
4 changes: 2 additions & 2 deletions scipy/io/tests/test_mmio.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ def test_bzip2_py3(self):
try:
# bz2 module isn't always built when building Python.
import bz2
except:
except ImportError:
return
I = array([0, 0, 1, 2, 3, 3, 3, 4])
J = array([0, 3, 1, 2, 1, 3, 4, 4])
Expand All @@ -582,7 +582,7 @@ def test_gzip_py3(self):
try:
# gzip module can be missing from Python installation
import gzip
except:
except ImportError:
return
I = array([0, 0, 1, 2, 3, 3, 3, 4])
J = array([0, 3, 1, 2, 1, 3, 4, 4])
Expand Down
2 changes: 1 addition & 1 deletion scipy/io/tests/test_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_read_write_files():
check_simple(f)
assert_equal(f.variables['app_var'][:], 42)

except:
except: # noqa: E722
os.chdir(cwd)
shutil.rmtree(tmpdir)
raise
Expand Down
Loading

0 comments on commit 0aca113

Please sign in to comment.