Repository navigation
MAINT: Tidy exception chaining in _datasource.py - #16761
Conversation
| shutil.copyfileobj(openedurl, f) | ||
| except URLError: | ||
| raise URLError("URL not found: %s" % path) | ||
| except URLError as e: |
There was a problem hiding this comment.
Can you show what the resulting error message is with this change?
There was a problem hiding this comment.
- @eric-wieser Before invalid URL exeption ( I run this on my personal conda environment ) was
Traceback (most recent call last):
File "/home/sarthak/anaconda3/lib/python3.7/site-packages/numpy/lib/_datasource.py", line 418, in _cache
with closing(urlopen(path)) as openedurl:
File "/home/sarthak/anaconda3/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/home/sarthak/anaconda3/lib/python3.7/urllib/request.py", line 531, in open
response = meth(req, response)
File "/home/sarthak/anaconda3/lib/python3.7/urllib/request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "/home/sarthak/anaconda3/lib/python3.7/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/home/sarthak/anaconda3/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/home/sarthak/anaconda3/lib/python3.7/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/sarthak/anaconda3/lib/python3.7/site-packages/numpy/lib/_datasource.py", line 422, in _cache
raise URLError("URL not found: %s" % path)
urllib.error.URLError: <urlopen error URL not found: https://raw.githubusercontent.com/numpy/numpy/master/toxsadas.ini>
- After the change
Traceback (most recent call last):
File "/home/sarthak/Desktop/projects/numpy/numpy/lib/_datasource.py", line 337, in _cache
with closing(urlopen(path)) as openedurl:
File "/usr/lib/python3.8/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.8/urllib/request.py", line 531, in open
response = meth(req, response)
File "/usr/lib/python3.8/urllib/request.py", line 640, in http_response
response = self.parent.error(
File "/usr/lib/python3.8/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/usr/lib/python3.8/urllib/request.py", line 502, in _call_chain
result = func(*args)
File "/usr/lib/python3.8/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/sarthak/Desktop/projects/numpy/numpy/lib/_datasource.py", line 341, in _cache
raise URLError("URL not found: %s" % path) from e
urllib.error.URLError: <urlopen error URL not found: https://raw.githubusercontent.com/numpy/numpy/master/toxsadas.ini>
eric-wieser
left a comment
There was a problem hiding this comment.
Definitely an improvement, but I wonder if we just want to use the inner exception by itself, and remove the try/except. Something for a follow-up PR, perhaps.
@eric-wieser Correct me if i am wrong 😕 , the error should look something like ( after removing try/except) |
eric-wieser
left a comment
There was a problem hiding this comment.
Great, thanks - looking back at the history, it looks like the second error message was around forever (0ce4792) and probably wasn't very deliberate.
| shutil.copyfileobj(openedurl, f) | ||
| except URLError: | ||
| raise URLError("URL not found: %s" % path) | ||
| with closing(urlopen(path)) as openedurl: |
There was a problem hiding this comment.
I bet closing isn't needed any more either...
There was a problem hiding this comment.
Yes, making another commit also removing from contextlib import closing it is not used anywhere else
Thank you
| shutil.copyfileobj(openedurl, f) | ||
| except URLError: | ||
| raise URLError("URL not found: %s" % path) | ||
| openedurl = urlopen(path) |
There was a problem hiding this comment.
The with statement is still needed, even if closing is not.
|
Thanks @sarthakchaudhary13 and @eric-wieser for the review! |
|
@eric-wieser and @rossbar thanks for helping me making my first contribution in numpy I am hoping to do more 😄 |
this solution is related to the following issue #15986
@eric-wieser this is my first contribution, can you review this pr?
Thank you.