Skip to content

Commit f80e5ce

Browse files
committed
BF(PY3): just catch all Exceptions, patch builtins module now provided in py3k
1 parent 45a6e87 commit f80e5ce

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

nibabel/optpkg.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,11 @@ def optional_package(name, trip_msg=None, min_version=None):
9797
exc = None
9898
try:
9999
pkg = __import__(name, fromlist=fromlist)
100-
except ImportError as exc:
101-
pass
102-
except Exception as exc: # it failed to import for some other reason
100+
except Exception as exc_:
101+
# Could fail due to some ImportError or for some other reason
103102
# e.g. h5py might have been checking file system to support UTF-8
104103
# etc. We should not blow if they blow
105-
pass
104+
exc = exc_ # So it is accessible outside of the code block
106105
else: # import worked
107106
# top level module
108107
if check_version(pkg):

nibabel/py3k.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def open_latin1(filename, mode='r'):
4141
ints2bytes = lambda seq: bytes(seq)
4242
ZEROB = bytes([0])
4343
FileNotFoundError = FileNotFoundError
44+
import builtins
4445
else:
4546
import StringIO
4647
StringIO = BytesIO = StringIO.StringIO
@@ -66,6 +67,8 @@ def open_latin1(filename, mode='r'):
6667
class FileNotFoundError(IOError):
6768
pass
6869

70+
import __builtin__ as builtins
71+
6972

7073
def getexception():
7174
return sys.exc_info()[1]

nibabel/tests/test_optpkg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
assert_equal)
1212

1313

14+
from nibabel.py3k import builtins
1415
from nibabel.optpkg import optional_package
1516
from nibabel.tripwire import TripWire, TripWireError
1617

@@ -41,7 +42,7 @@ def raise_Exception(*args, **kwargs):
4142
raise Exception(
4243
"non ImportError could be thrown by some malfunctioning module "
4344
"upon import, and optional_package should catch it too")
44-
with mock.patch('__builtin__.__import__', side_effect=raise_Exception):
45+
with mock.patch.object(builtins, '__import__', side_effect=raise_Exception):
4546
assert_bad('nottriedbefore')
4647

4748

0 commit comments

Comments
 (0)