summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorMatti Picus <matti.picus@gmail.com>2019-06-06 09:32:05 +0300
committerGitHub <noreply@github.com>2019-06-06 09:32:05 +0300
commit7dc91dea8adf8c8e1c551dc8debfcacdd1d5d11a (patch)
tree9971e9b799f54b07c364d3aff8c7faba546a955e /numpy
parent07c625cedd18d817b0941e39b4fe6a13caf034a1 (diff)
parentb0399b905d5813529d068f6cb9a6cd8376b684bd (diff)
downloadnumpy-7dc91dea8adf8c8e1c551dc8debfcacdd1d5d11a.tar.gz
Merge pull request #13720 from eric-wieser/more-with-statements
MAINT/BUG: Manage more files with with statements
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/code_generators/genapi.py5
-rw-r--r--numpy/core/records.py58
-rw-r--r--numpy/core/setup_common.py30
-rw-r--r--numpy/distutils/system_info.py35
-rw-r--r--numpy/lib/_datasource.py11
-rw-r--r--numpy/lib/format.py10
6 files changed, 63 insertions, 86 deletions
diff --git a/numpy/core/code_generators/genapi.py b/numpy/core/code_generators/genapi.py
index 4aca2373c..923c34425 100644
--- a/numpy/core/code_generators/genapi.py
+++ b/numpy/core/code_generators/genapi.py
@@ -483,14 +483,11 @@ def get_versions_hash():
d = []
file = os.path.join(os.path.dirname(__file__), 'cversions.txt')
- fid = open(file, 'r')
- try:
+ with open(file, 'r') as fid:
for line in fid:
m = VERRE.match(line)
if m:
d.append((int(m.group(1), 16), m.group(2)))
- finally:
- fid.close()
return dict(d)
diff --git a/numpy/core/records.py b/numpy/core/records.py
index 2adcdae61..659ffa42b 100644
--- a/numpy/core/records.py
+++ b/numpy/core/records.py
@@ -42,7 +42,9 @@ from collections import Counter, OrderedDict
from . import numeric as sb
from . import numerictypes as nt
-from numpy.compat import isfileobj, bytes, long, unicode, os_fspath
+from numpy.compat import (
+ isfileobj, bytes, long, unicode, os_fspath, contextlib_nullcontext
+)
from numpy.core.overrides import set_module
from .arrayprint import get_printoptions
@@ -777,44 +779,42 @@ def fromfile(fd, dtype=None, shape=None, offset=0, formats=None,
if isfileobj(fd):
# file already opened
- name = 0
+ ctx = contextlib_nullcontext(fd)
else:
# open file
- fd = open(os_fspath(fd), 'rb')
- name = 1
+ ctx = open(os_fspath(fd), 'rb')
- if (offset > 0):
- fd.seek(offset, 1)
- size = get_remaining_size(fd)
+ with ctx as fd:
+ if (offset > 0):
+ fd.seek(offset, 1)
+ size = get_remaining_size(fd)
- if dtype is not None:
- descr = sb.dtype(dtype)
- else:
- descr = format_parser(formats, names, titles, aligned, byteorder)._descr
+ if dtype is not None:
+ descr = sb.dtype(dtype)
+ else:
+ descr = format_parser(formats, names, titles, aligned, byteorder)._descr
- itemsize = descr.itemsize
+ itemsize = descr.itemsize
- shapeprod = sb.array(shape).prod(dtype=nt.intp)
- shapesize = shapeprod * itemsize
- if shapesize < 0:
- shape = list(shape)
- shape[shape.index(-1)] = size // -shapesize
- shape = tuple(shape)
shapeprod = sb.array(shape).prod(dtype=nt.intp)
+ shapesize = shapeprod * itemsize
+ if shapesize < 0:
+ shape = list(shape)
+ shape[shape.index(-1)] = size // -shapesize
+ shape = tuple(shape)
+ shapeprod = sb.array(shape).prod(dtype=nt.intp)
- nbytes = shapeprod * itemsize
+ nbytes = shapeprod * itemsize
- if nbytes > size:
- raise ValueError(
- "Not enough bytes left in file for specified shape and type")
+ if nbytes > size:
+ raise ValueError(
+ "Not enough bytes left in file for specified shape and type")
- # create the array
- _array = recarray(shape, descr)
- nbytesread = fd.readinto(_array.data)
- if nbytesread != nbytes:
- raise IOError("Didn't read as many bytes as expected")
- if name:
- fd.close()
+ # create the array
+ _array = recarray(shape, descr)
+ nbytesread = fd.readinto(_array.data)
+ if nbytesread != nbytes:
+ raise IOError("Didn't read as many bytes as expected")
return _array
diff --git a/numpy/core/setup_common.py b/numpy/core/setup_common.py
index 32d52d93e..bf6abcf02 100644
--- a/numpy/core/setup_common.py
+++ b/numpy/core/setup_common.py
@@ -312,30 +312,24 @@ def pyod(filename):
def _pyod2():
out = []
- fid = open(filename, 'rb')
- try:
+ with open(filename, 'rb') as fid:
yo = [int(oct(int(binascii.b2a_hex(o), 16))) for o in fid.read()]
- for i in range(0, len(yo), 16):
- line = ['%07d' % int(oct(i))]
- line.extend(['%03d' % c for c in yo[i:i+16]])
- out.append(" ".join(line))
- return out
- finally:
- fid.close()
+ for i in range(0, len(yo), 16):
+ line = ['%07d' % int(oct(i))]
+ line.extend(['%03d' % c for c in yo[i:i+16]])
+ out.append(" ".join(line))
+ return out
def _pyod3():
out = []
- fid = open(filename, 'rb')
- try:
+ with open(filename, 'rb') as fid:
yo2 = [oct(o)[2:] for o in fid.read()]
- for i in range(0, len(yo2), 16):
- line = ['%07d' % int(oct(i)[2:])]
- line.extend(['%03d' % int(c) for c in yo2[i:i+16]])
- out.append(" ".join(line))
- return out
- finally:
- fid.close()
+ for i in range(0, len(yo2), 16):
+ line = ['%07d' % int(oct(i)[2:])]
+ line.extend(['%03d' % int(c) for c in yo2[i:i+16]])
+ out.append(" ".join(line))
+ return out
if sys.version_info[0] < 3:
return _pyod2()
diff --git a/numpy/distutils/system_info.py b/numpy/distutils/system_info.py
index ba4ad4643..f169dba59 100644
--- a/numpy/distutils/system_info.py
+++ b/numpy/distutils/system_info.py
@@ -301,26 +301,21 @@ else:
default_x11_include_dirs.extend(['/usr/lib/X11/include',
'/usr/include/X11'])
- tmp = None
- try:
- # Explicitly open/close file to avoid ResourceWarning when
- # tests are run in debug mode Python 3.
- tmp = open(os.devnull, 'w')
- p = subprocess.Popen(["gcc", "-print-multiarch"], stdout=subprocess.PIPE,
- stderr=tmp)
- except (OSError, DistutilsError):
- # OSError if gcc is not installed, or SandboxViolation (DistutilsError
- # subclass) if an old setuptools bug is triggered (see gh-3160).
- pass
- else:
- triplet = str(p.communicate()[0].decode().strip())
- if p.returncode == 0:
- # gcc supports the "-print-multiarch" option
- default_x11_lib_dirs += [os.path.join("/usr/lib/", triplet)]
- default_lib_dirs += [os.path.join("/usr/lib/", triplet)]
- finally:
- if tmp is not None:
- tmp.close()
+ with open(os.devnull, 'w') as tmp:
+ try:
+ p = subprocess.Popen(["gcc", "-print-multiarch"], stdout=subprocess.PIPE,
+ stderr=tmp)
+ except (OSError, DistutilsError):
+ # OSError if gcc is not installed, or SandboxViolation (DistutilsError
+ # subclass) if an old setuptools bug is triggered (see gh-3160).
+ pass
+ else:
+ triplet = str(p.communicate()[0].decode().strip())
+ if p.returncode == 0:
+ # gcc supports the "-print-multiarch" option
+ default_x11_lib_dirs += [os.path.join("/usr/lib/", triplet)]
+ default_lib_dirs += [os.path.join("/usr/lib/", triplet)]
+
if os.path.join(sys.prefix, 'lib') not in default_lib_dirs:
default_lib_dirs.insert(0, os.path.join(sys.prefix, 'lib'))
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:
diff --git a/numpy/lib/format.py b/numpy/lib/format.py
index cd8700051..f5f3b28d3 100644
--- a/numpy/lib/format.py
+++ b/numpy/lib/format.py
@@ -841,16 +841,12 @@ def open_memmap(filename, mode='r+', dtype=None, shape=None,
shape=shape,
)
# If we got here, then it should be safe to create the file.
- fp = open(os_fspath(filename), mode+'b')
- try:
+ with open(os_fspath(filename), mode+'b') as fp:
_write_array_header(fp, d, version)
offset = fp.tell()
- finally:
- fp.close()
else:
# Read the header of the file first.
- fp = open(os_fspath(filename), 'rb')
- try:
+ with open(os_fspath(filename), 'rb') as fp:
version = read_magic(fp)
_check_version(version)
@@ -859,8 +855,6 @@ def open_memmap(filename, mode='r+', dtype=None, shape=None,
msg = "Array can't be memory-mapped: Python objects in dtype."
raise ValueError(msg)
offset = fp.tell()
- finally:
- fp.close()
if fortran_order:
order = 'F'