summaryrefslogtreecommitdiff
path: root/numpy/lib/_datasource.py
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2019-06-04 23:06:17 -0700
committerEric Wieser <wieser.eric@gmail.com>2019-06-04 23:26:42 -0700
commit06a32ea32a0a69990e6ca936c810bceab808ebd0 (patch)
tree34e42c398053df906e266a7cb3475de7d836d4ee /numpy/lib/_datasource.py
parent8826e0ffc2c5286572dff1d490bcda88a6f7cd64 (diff)
downloadnumpy-06a32ea32a0a69990e6ca936c810bceab808ebd0.tar.gz
BUG: Ensure that the url request is closed if the file cannot be opened
Diffstat (limited to 'numpy/lib/_datasource.py')
-rw-r--r--numpy/lib/_datasource.py11
1 files changed, 4 insertions, 7 deletions
diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py
index 816f7624e..0d71375c2 100644
--- a/numpy/lib/_datasource.py
+++ b/numpy/lib/_datasource.py
@@ -41,6 +41,7 @@ import sys
import warnings
import shutil
import io
+from contextlib import closing
from numpy.core.overrides import set_module
@@ -414,13 +415,9 @@ class DataSource(object):
# TODO: Doesn't handle compressed files!
if self._isurl(path):
try:
- openedurl = urlopen(path)
- f = _open(upath, 'wb')
- try:
- shutil.copyfileobj(openedurl, f)
- finally:
- f.close()
- openedurl.close()
+ with closing(urlopen(path)) as openedurl:
+ with _open(upath, 'wb') as f:
+ shutil.copyfileobj(openedurl, f)
except URLError:
raise URLError("URL not found: %s" % path)
else: