Skip to content

MAINT: Tidy exception chaining in _datasource.py - #16761

Merged
rossbar merged 4 commits into
numpy:masterfrom
sarthakvk:fix-exception-causes
Jul 8, 2020
Merged

rossbar merged 4 commits into
numpy:masterfrom
sarthakvk:fix-exception-causes

Conversation

@sarthakvk

@sarthakvk sarthakvk commented Jul 6, 2020 •

Copy link
Copy Markdown
Contributor

this solution is related to the following issue #15986
@eric-wieser this is my first contribution, can you review this pr?
Thank you.

@charris charris changed the title ENH: Fix exception causes in _datasource.py MAINT: Chain exception in _datasource.py Jul 6, 2020
Comment thread numpy/lib/_datasource.py Outdated
shutil.copyfileobj(openedurl, f)
except URLError:
raise URLError("URL not found: %s" % path)
except URLError as e:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you show what the resulting error message is with this change?

@sarthakvk sarthakvk Jul 6, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  • @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 eric-wieser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@sarthakvk

sarthakvk commented Jul 6, 2020 •

Copy link
Copy Markdown
Contributor Author

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)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sarthak/Desktop/projects/numpy/numpy/lib/_datasource.py", line 336, 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

@eric-wieser eric-wieser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great, thanks - looking back at the history, it looks like the second error message was around forever (0ce4792) and probably wasn't very deliberate.

Comment thread numpy/lib/_datasource.py Outdated
shutil.copyfileobj(openedurl, f)
except URLError:
raise URLError("URL not found: %s" % path)
with closing(urlopen(path)) as openedurl:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I bet closing isn't needed any more either...

@sarthakvk sarthakvk Jul 7, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, making another commit also removing from contextlib import closing it is not used anywhere else
Thank you

@sarthakvk
sarthakvk requested a review from eric-wieser July 7, 2020 18:46
Comment thread numpy/lib/_datasource.py Outdated
shutil.copyfileobj(openedurl, f)
except URLError:
raise URLError("URL not found: %s" % path)
openedurl = urlopen(path)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The with statement is still needed, even if closing is not.

@sarthakvk
sarthakvk requested a review from eric-wieser July 8, 2020 19:20
@eric-wieser eric-wieser changed the title MAINT: Chain exception in _datasource.py MAINT: Tidy exception chaining in _datasource.py Jul 8, 2020
@rossbar
rossbar merged commit 996e641 into numpy:master Jul 8, 2020
@rossbar

rossbar commented Jul 8, 2020

Copy link
Copy Markdown
Contributor

Thanks @sarthakchaudhary13 and @eric-wieser for the review!

@sarthakvk

Copy link
Copy Markdown
Contributor Author

@eric-wieser and @rossbar thanks for helping me making my first contribution in numpy I am hoping to do more 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants