Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception causes all over the codebase #5787

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python-package/xgboost/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ def os_fspath(path):
path_type = type(path)
try:
path_repr = path_type.__fspath__(path)
except AttributeError:
except AttributeError as e:
if hasattr(path_type, '__fspath__'):
raise
if issubclass(path_type, PurePath):
return _PurePath__fspath__(path)
raise TypeError("expected str, bytes or os.PathLike object, "
"not " + path_type.__name__)
"not " + path_type.__name__) from e
if isinstance(path_repr, (str, bytes)):
return path_repr
raise TypeError("expected {}.__fspath__() to return str or bytes, "
Expand Down
8 changes: 4 additions & 4 deletions python-package/xgboost/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,19 @@ def _convert_unknown_data(data, meta=None, meta_type=None):
if meta is not None:
try:
data = np.array(data, dtype=meta_type)
except Exception:
except Exception as e:
raise TypeError('Can not handle data from {}'.format(
type(data).__name__))
type(data).__name__)) from e
else:
import warnings
warnings.warn(
'Unknown data type: ' + str(type(data)) +
', coverting it to csr_matrix')
try:
data = scipy.sparse.csr_matrix(data)
except Exception:
except Exception as e:
raise TypeError('Can not initialize DMatrix from'
' {}'.format(type(data).__name__))
' {}'.format(type(data).__name__)) from e
return data


Expand Down
12 changes: 6 additions & 6 deletions python-package/xgboost/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def plot_importance(booster, ax=None, height=0.2,
"""
try:
import matplotlib.pyplot as plt
except ImportError:
raise ImportError('You must install matplotlib to plot importance')
except ImportError as e:
raise ImportError('You must install matplotlib to plot importance') from e

if isinstance(booster, XGBModel):
importance = booster.get_booster().get_score(
Expand Down Expand Up @@ -168,8 +168,8 @@ def to_graphviz(booster, fmap='', num_trees=0, rankdir=None,
"""
try:
from graphviz import Source
except ImportError:
raise ImportError('You must install graphviz to plot tree')
except ImportError as e:
raise ImportError('You must install graphviz to plot tree') from e
if isinstance(booster, XGBModel):
booster = booster.get_booster()

Expand Down Expand Up @@ -237,8 +237,8 @@ def plot_tree(booster, fmap='', num_trees=0, rankdir=None, ax=None, **kwargs):
try:
from matplotlib import pyplot as plt
from matplotlib import image
except ImportError:
raise ImportError('You must install matplotlib to plot tree')
except ImportError as e:
raise ImportError('You must install matplotlib to plot tree') from e

if ax is None:
_, ax = plt.subplots(1, 1)
Expand Down