Skip to content

Commit 038f926

Browse files
committed
Do not use bare except: clauses
https://docs.python.org/2/howto/doanddont.html#except Signed-off-by: Anders Kaseorg <andersk@mit.edu>
1 parent 32196a0 commit 038f926

File tree

9 files changed

+25
-21
lines changed

9 files changed

+25
-21
lines changed

arrayfire/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
try:
5252
import pycuda.autoinit
53-
except:
53+
except ImportError:
5454
pass
5555

5656
from .library import *

arrayfire/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ def display(a, precision=4):
13101310
st = expr[0].find('(') + 1
13111311
en = expr[0].rfind(')')
13121312
name = expr[0][st:en]
1313-
except:
1313+
except IndexError:
13141314
pass
13151315

13161316
safe_call(backend.get().af_print_array_gen(name.encode('utf-8'),

arrayfire/interop.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def _cc_to_af_array(in_ptr, ndim, in_shape, in_dtype, is_device=False, copy = Tr
7575

7676
try:
7777
import numpy as np
78+
except ImportError:
79+
AF_NUMPY_FOUND=False
80+
else:
7881
from numpy import ndarray as NumpyArray
7982
from .data import reorder
8083

@@ -112,11 +115,12 @@ def np_to_af_array(np_arr, copy=True):
112115
return np_to_af_array(np_arr.copy())
113116

114117
from_ndarray = np_to_af_array
115-
except:
116-
AF_NUMPY_FOUND=False
117118

118119
try:
119120
import pycuda.gpuarray
121+
except ImportError:
122+
AF_PYCUDA_FOUND=False
123+
else:
120124
from pycuda.gpuarray import GPUArray as CudaArray
121125
AF_PYCUDA_FOUND=True
122126

@@ -154,11 +158,12 @@ def pycuda_to_af_array(pycu_arr, copy=True):
154158
return _cc_to_af_array(in_ptr, pycu_arr.ndim, in_shape, in_dtype, True, copy)
155159
else:
156160
return pycuda_to_af_array(pycu_arr.copy())
157-
except:
158-
AF_PYCUDA_FOUND=False
159161

160162
try:
161163
from pyopencl.array import Array as OpenclArray
164+
except ImportError:
165+
AF_PYOPENCL_FOUND=False
166+
else:
162167
from .opencl import add_device_context as _add_device_context
163168
from .opencl import set_device_context as _set_device_context
164169
from .opencl import get_device_id as _get_device_id
@@ -221,11 +226,12 @@ def pyopencl_to_af_array(pycl_arr, copy=True):
221226
return _cc_to_af_array(in_ptr, pycl_arr.ndim, in_shape, in_dtype, True, copy)
222227
else:
223228
return pyopencl_to_af_array(pycl_arr.copy())
224-
except:
225-
AF_PYOPENCL_FOUND=False
226229

227230
try:
228231
import numba
232+
except ImportError:
233+
AF_NUMBA_FOUND=False
234+
else:
229235
from numba import cuda
230236
NumbaCudaArray = cuda.cudadrv.devicearray.DeviceNDArray
231237
AF_NUMBA_FOUND=True
@@ -264,8 +270,6 @@ def numba_to_af_array(nb_arr, copy=True):
264270
return _cc_to_af_array(in_ptr, nb_arr.ndim, in_shape, in_dtype, True, copy)
265271
else:
266272
return numba_to_af_array(nb_arr.copy())
267-
except:
268-
AF_NUMBA_FOUND=False
269273

270274
def to_array(in_array, copy = True):
271275
"""

arrayfire/library.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from enum import Enum as _Enum
4848
def _Enum_Type(v):
4949
return v
50-
except:
50+
except ImportError:
5151
class _MetaEnum(type):
5252
def __init__(cls, name, bases, attrs):
5353
for attrname, attrvalue in attrs.iteritems():
@@ -417,15 +417,15 @@ def _setup():
417417

418418
try:
419419
AF_PATH = os.environ['AF_PATH']
420-
except:
420+
except KeyError:
421421
AF_PATH = None
422422
pass
423423

424424
AF_SEARCH_PATH = AF_PATH
425425

426426
try:
427427
CUDA_PATH = os.environ['CUDA_PATH']
428-
except:
428+
except KeyError:
429429
CUDA_PATH= None
430430
pass
431431

@@ -534,7 +534,7 @@ def __init__(self):
534534
for libname in libnames:
535535
try:
536536
ct.cdll.LoadLibrary(libname)
537-
except:
537+
except OSError:
538538
pass
539539

540540
c_dim4 = c_dim_t*4
@@ -555,7 +555,7 @@ def __init__(self):
555555
self.__name = __name
556556
clib.af_release_array(out)
557557
break;
558-
except:
558+
except OSError:
559559
pass
560560

561561
if (self.__name is None):

arrayfire/tests/simple/_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def run(self, name_list=None, verbose=False):
2424
self.print_log = ''
2525
try:
2626
test = self[key]
27-
except:
27+
except KeyError:
2828
print(self.print_str % (key, "NOTFOUND"))
2929
continue
3030

examples/benchmarks/bench_blas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
try:
1818
import numpy as np
19-
except:
19+
except ImportError:
2020
np = None
2121

2222

examples/benchmarks/bench_cg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
try:
1818
import numpy as np
19-
except:
19+
except ImportError:
2020
np = None
2121

2222
try:
2323
from scipy import sparse as sp
2424
from scipy.sparse import linalg
25-
except:
25+
except ImportError:
2626
sp = None
2727

2828

examples/benchmarks/bench_fft.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
try:
1818
import numpy as np
19-
except:
19+
except ImportError:
2020
np = None
2121

2222

examples/benchmarks/monte_carlo_pi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
try:
1818
import numpy as np
19-
except:
19+
except ImportError:
2020
np = None
2121

2222
#alias range / xrange because xrange is faster than range in python2

0 commit comments

Comments
 (0)