-
-
Notifications
You must be signed in to change notification settings - Fork 11k
ENH: Fix exception causes in _iotools.py #15731
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -699,12 +699,12 @@ def _strict_call(self, value): | |
# We're still here so we can now return the new value | ||
return new_value | ||
|
||
except ValueError: | ||
except ValueError as e: | ||
if value.strip() in self.missing_values: | ||
if not self._status: | ||
self._checked = False | ||
return self.default | ||
raise ValueError("Cannot convert string '%s'" % value) | ||
raise ValueError("Cannot convert string '%s'" % value) from e | ||
# | ||
|
||
def __call__(self, value): | ||
|
@@ -735,17 +735,17 @@ def upgrade(self, value): | |
self._checked = True | ||
try: | ||
return self._strict_call(value) | ||
except ValueError: | ||
except ValueError as e: | ||
# Raise an exception if we locked the converter... | ||
if self._locked: | ||
errmsg = "Converter is locked and cannot be upgraded" | ||
raise ConverterLockError(errmsg) | ||
raise ConverterLockError(errmsg) from e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exception chain is a bit noisy, but it looks like the early exceptions have useful information, so I guess this use of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is correct. This makes the error message However, that's not what's happening here. What's happening here is that something else went wrong while we tried to recover (by upgrading the data type). Today, the message is
This is a more accurate message. So this line was better unchanged. |
||
_statusmax = len(self._mapper) | ||
# Complains if we try to upgrade by the maximum | ||
_status = self._status | ||
if _status == _statusmax: | ||
errmsg = "Could not find a valid conversion function" | ||
raise ConverterError(errmsg) | ||
raise ConverterError(errmsg) from e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it is in the same situation as the previous one: it makes a noisy exception, but there might be useful info. in there, so OK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
elif _status < _statusmax - 1: | ||
_status += 1 | ||
(self.type, self.func, default) = self._mapper[_status] | ||
|
@@ -764,18 +764,18 @@ def iterupgrade(self, value): | |
try: | ||
for _m in value: | ||
_strict_call(_m) | ||
except ValueError: | ||
except ValueError as e: | ||
# Raise an exception if we locked the converter... | ||
if self._locked: | ||
errmsg = "Converter is locked and cannot be upgraded" | ||
raise ConverterLockError(errmsg) | ||
raise ConverterLockError(errmsg) from e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto, so OK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
_statusmax = len(self._mapper) | ||
# Complains if we try to upgrade by the maximum | ||
_status = self._status | ||
if _status == _statusmax: | ||
raise ConverterError( | ||
"Could not find a valid conversion function" | ||
) | ||
) from e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto, so OK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
elif _status < _statusmax - 1: | ||
_status += 1 | ||
(self.type, self.func, default) = self._mapper[_status] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the previous exception in the will give include the details of what went wrong. This one just says, in effect "fail!". So I guess this use of
from e
is OK.(I'm starting to think that the way this code uses exceptions is awkward, and could use a redesign, but that will have to wait for another time.)