8000 ENH: Fix exception causes in _iotools.py by cool-RR · Pull Request #15731 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 8 additions & 8 deletions numpy/lib/_iotools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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

#

def __call__(self, value):
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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 from e is OK:

In [121]: conv = StringConverter(int, locked=True)

In [122]: conv.upgrade('0.')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/mc37np/lib/python3.7/site-packages/numpy-1.19.0.dev0+116a021-py3.7-macosx-10.9-x86_64.egg/numpy/lib/_iotools.py in _strict_call(self, value)
    687             # We check if we can convert the value using the current function
--> 688             new_value = self.func(value)
    689 

ValueError: invalid literal for int() with base 10: '0.'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
~/mc37np/lib/python3.7/site-packages/numpy-1.19.0.dev0+116a021-py3.7-macosx-10.9-x86_64.egg/numpy/lib/_iotools.py in upgrade(self, value)
    736         try:
--> 737             return self._strict_call(value)
    738         except ValueError as e:

~/mc37np/lib/python3.7/site-packages/numpy-1.19.0.dev0+116a021-py3.7-macosx-10.9-x86_64.egg/numpy/lib/_iotools.py in _strict_call(self, value)
    706                 return self.default
--> 707             raise ValueError("Cannot convert string '%s'" % value) from e
    708     #

ValueError: Cannot convert string '0.'

The above exception was the direct cause of the following exception:

ConverterLockError                        Traceback (most recent call last)
<ipython-input-122-b82e0b39004c> in <module>
----> 1 conv.upgrade('0.')

~/mc37np/lib/python3.7/site-packages/numpy-1.19.0.dev0+116a021-py3.7-macosx-10.9-x86_64.egg/numpy/lib/_iotools.py in upgrade(self, value)
    740             if self._locked:
    741                 errmsg = "Converter is locked and cannot be upgraded"
--> 742                 raise ConverterLockError(errmsg) from e
    743             _statusmax = len(self._mapper)
    744             # Complains if we try to upgrade by the maximum

ConverterLockError: Converter is locked and cannot be upgraded

Copy link
Member
@eric-wieser eric-wieser Mar 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct.

This makes the error message The above exception was the direct cause of the following exception:.

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

During handling of the above exception, another exception occurred:

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
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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]
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, so OK.

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, so OK.

Copy link
Member

Choose a reason for hiding this comment

The 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]
Expand Down
0