diff options
Diffstat (limited to 'numpy/lib/_datasource.py')
-rw-r--r-- | numpy/lib/_datasource.py | 11 |
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: |