Skip to content

Commit 363ac67

Browse files
author
Shoshana Berleant
committed
use raise_from instead of raise to keep stack trace
1 parent 58dddfd commit 363ac67

File tree

9 files changed

+47
-35
lines changed

9 files changed

+47
-35
lines changed

nipype/algorithms/misc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from __future__ import division
1616
from builtins import zip
1717
from builtins import range
18+
from future.utils import raise_from
1819

1920
import os
2021
import os.path as op
@@ -857,9 +858,9 @@ def __init__(self, infields=None, force_run=True, **kwargs):
857858
def _run_interface(self, runtime):
858859
try:
859860
import pandas as pd
860-
except ImportError:
861-
raise ImportError(('This interface requires pandas '
862-
'(http://pandas.pydata.org/) to run.'))
861+
except ImportError as e:
862+
raise_from(ImportError('This interface requires pandas '
863+
'(http://pandas.pydata.org/) to run.'), e)
863864

864865
try:
865866
import lockfile as pl

nipype/external/six.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# SOFTWARE.
2222

2323
from __future__ import absolute_import
24+
from future.utils import raise_from
2425

2526
import functools
2627
import itertools
@@ -186,8 +187,8 @@ def find_module(self, fullname, path=None):
186187
def __get_module(self, fullname):
187188
try:
188189
return self.known_modules[fullname]
189-
except KeyError:
190-
raise ImportError("This loader does not know module " + fullname)
190+
except KeyError as e:
191+
raise_from(ImportError("This loader does not know module " + fullname), e)
191192

192193
def load_module(self, fullname):
193194
try:

nipype/interfaces/afni/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
from sys import platform
77
from builtins import object
8+
from future.utils import raise_from
89

910
from ... import logging
1011
from ...utils.filemanip import split_filename
@@ -82,9 +83,9 @@ def outputtype_to_ext(cls, outputtype):
8283

8384
try:
8485
return cls.ftypes[outputtype]
85-
except KeyError:
86+
except KeyError as e:
8687
msg = 'Invalid AFNIOUTPUTTYPE: ', outputtype
87-
raise KeyError(msg)
88+
raise_from(KeyError(msg), e)
8889

8990
@classmethod
9091
def outputtype(cls):

nipype/pipeline/plugins/ipython.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from future import standard_library
77
standard_library.install_aliases()
8+
from future.utils import raise_from
89

910
from pickle import dumps
1011

@@ -63,19 +64,20 @@ def run(self, graph, config, updatehash=False):
6364
name = 'ipyparallel'
6465
__import__(name)
6566
self.iparallel = sys.modules[name]
66-
except ImportError:
67-
raise ImportError("Ipython kernel not found. Parallel execution "
68-
"will be unavailable")
67+
except ImportError as e:
68+
raise_from(ImportError("Ipython kernel not found. Parallel execution "
69+
"will be unavailable"), e)
6970
try:
7071
self.taskclient = self.iparallel.Client()
7172
except Exception as e:
7273
if isinstance(e, TimeoutError):
73-
raise Exception("No IPython clients found.")
74+
raise_from(Exception("No IPython clients found."), e)
7475
if isinstance(e, IOError):
75-
raise Exception("ipcluster/ipcontroller has not been started")
76+
raise_from(Exception("ipcluster/ipcontroller has not been started"), e)
7677
if isinstance(e, ValueError):
77-
raise Exception("Ipython kernel not installed")
78-
raise e
78+
raise_from(Exception("Ipython kernel not installed"), e)
79+
else:
80+
raise e
7981
return super(IPythonPlugin, self).run(graph, config, updatehash=updatehash)
8082

8183
def _get_result(self, taskid):

nipype/pipeline/plugins/ipythonx.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import sys
7+
from future.utils import raise_from
78

89
IPython_not_loaded = False
910
try:
@@ -36,16 +37,16 @@ def run(self, graph, config, updatehash=False):
3637
name = 'IPython.kernel.client'
3738
__import__(name)
3839
self.ipyclient = sys.modules[name]
39-
except ImportError:
40-
raise ImportError("Ipython kernel not found. Parallel execution "
41-
"will be unavailable")
40+
except ImportError as e:
41+
raise_from(ImportError("Ipython kernel not found. Parallel execution "
42+
"will be unavailable"), e)
4243
try:
4344
self.taskclient = self.ipyclient.TaskClient()
4445
except Exception as e:
4546
if isinstance(e, ConnectionRefusedError):
46-
raise Exception("No IPython clients found.")
47+
raise_from(Exception("No IPython clients found."), e)
4748
if isinstance(e, ValueError):
48-
raise Exception("Ipython kernel not installed")
49+
raise_from(Exception("Ipython kernel not installed"), e)
4950
return super(IPythonXPlugin, self).run(graph, config, updatehash=updatehash)
5051

5152
def _get_result(self, taskid):

nipype/testing/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from tempfile import mkdtemp
1313
from ..utils.misc import package_check
1414
from nose import SkipTest
15-
15+
from future.utils import raise_from
1616

1717
def skip_if_no_package(*args, **kwargs):
1818
"""Raise SkipTest if package_check fails
@@ -62,15 +62,15 @@ def __init__(self, size_in_mbytes=8, delay=0.5):
6262
try:
6363
subprocess.check_call(args=mkfs_args, stdout=self.dev_null,
6464
stderr=self.dev_null)
65-
except subprocess.CalledProcessError:
66-
raise IOError("mkfs.vfat failed")
65+
except subprocess.CalledProcessError as e:
66+
raise_from(IOError("mkfs.vfat failed"), e)
6767

6868
try:
6969
self.fusefat = subprocess.Popen(args=mount_args,
7070
stdout=self.dev_null,
7171
stderr=self.dev_null)
72-
except OSError:
73-
raise IOError("fusefat is not installed")
72+
except OSError as e:
73+
raise_from(IOError("fusefat is not installed"), e)
7474

7575
time.sleep(self.delay)
7676

nipype/utils/misc.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from future import standard_library
77
standard_library.install_aliases()
8+
from future.utils import raise_from
89
from builtins import next
910
from pickle import dumps, loads
1011
import inspect
@@ -90,14 +91,14 @@ def create_function_from_source(function_source, imports=None):
9091
import_keys = list(ns.keys())
9192
exec(function_source, ns)
9293

93-
except Exception as msg:
94-
msg = str(msg) + '\nError executing function:\n %s\n' % function_source
94+
except Exception as e:
95+
msg = '\nError executing function:\n %s\n' % function_source
9596
msg += '\n'.join(["Functions in connection strings have to be standalone.",
9697
"They cannot be declared either interactively or inside",
9798
"another function or inline in the connect string. Any",
9899
"imports should be done inside the function"
99100
])
100-
raise RuntimeError(msg)
101+
raise_from(RuntimeError(msg), e)
101102
ns_funcs = list(set(ns) - set(import_keys + ['__builtins__']))
102103
assert len(ns_funcs) == 1, "Function or inputs are ill-defined"
103104
funcname = ns_funcs[0]
@@ -199,14 +200,14 @@ def package_check(pkg_name, version=None, app=None, checker=LooseVersion,
199200
msg += ' with version >= %s' % (version,)
200201
try:
201202
mod = __import__(pkg_name)
202-
except ImportError:
203-
raise exc_failed_import(msg)
203+
except ImportError as e:
204+
raise_from(exc_failed_import(msg), e)
204205
if not version:
205206
return
206207
try:
207208
have_version = mod.__version__
208-
except AttributeError:
209-
raise exc_failed_check('Cannot find version for %s' % pkg_name)
209+
except AttributeError as e:
210+
raise_from(exc_failed_check('Cannot find version for %s' % pkg_name), e)
210211
if checker(have_version) < checker(version):
211212
raise exc_failed_check(msg)
212213

nipype/utils/spm_docs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os
66

7+
from future.utils import raise_from
78
from nipype.interfaces import matlab
89

910

@@ -55,5 +56,5 @@ def _strip_header(doc):
5556
except ValueError:
5657
index = len(doc)
5758
return doc[:index]
58-
except KeyError:
59-
raise IOError('This docstring was not generated by Nipype!\n')
59+
except KeyError as e:
60+
raise_from(IOError('This docstring was not generated by Nipype!\n'), e)

nipype/workflows/dmri/connectivity/group_connectivity.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
asdf;alksjahgldhjgioae
2+
3+
14
from __future__ import print_function
5+
from future.utils import raise_from
26

37
import os.path as op
48

@@ -459,9 +463,9 @@ def create_average_networks_by_group_workflow(group_list, data_dir, subjects_dir
459463
try:
460464
l4infosource.inputs.group_id1 = list(group_list.keys())[0]
461465
l4infosource.inputs.group_id2 = list(group_list.keys())[1]
462-
except IndexError:
466+
except IndexError as e:
463467
print('The create_average_networks_by_group_workflow requires 2 groups')
464-
raise Exception
468+
raise_from(Exception(), e)
465469

466470
l4info = dict(networks=[['group_id', '']], CMatrices=[['group_id', '']], fibmean=[['group_id', 'mean_fiber_length']],
467471
fibdev=[['group_id', 'fiber_length_std']])

0 commit comments

Comments
 (0)