Skip to content

Commit 9162487

Browse files
authored
Merge pull request #143 from andersk/bare-except
Do not use bare except: clauses
2 parents c481bd8 + 038f926 commit 9162487

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
@@ -51,7 +51,7 @@
5151
from enum import Enum as _Enum
5252
def _Enum_Type(v):
5353
return v
54-
except:
54+
except ImportError:
5555
class _MetaEnum(type):
5656
def __init__(cls, name, bases, attrs):
5757
for attrname, attrvalue in attrs.iteritems():
@@ -423,15 +423,15 @@ def _setup():
423423

424424
try:
425425
AF_PATH = os.environ['AF_PATH']
426-
except:
426+
except KeyError:
427427
AF_PATH = None
428428
pass
429429

430430
AF_SEARCH_PATH = AF_PATH
431431

432432
try:
433433
CUDA_PATH = os.environ['CUDA_PATH']
434-
except:
434+
except KeyError:
435435
CUDA_PATH= None
436436
pass
437437

@@ -541,7 +541,7 @@ def __init__(self):
541541
for libname in libnames:
542542
try:
543543
ct.cdll.LoadLibrary(libname)
544-
except:
544+
except OSError:
545545
pass
546546

547547
c_dim4 = c_dim_t*4
@@ -562,7 +562,7 @@ def __init__(self):
562562
self.__name = __name
563563
clib.af_release_array(out)
564564
break;
565-
except:
565+
except OSError:
566566
pass
567567

568568
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)