8000 Merge pull request #13720 from eric-wieser/more-with-statements · numpy/numpy@7dc91de · GitHub
[go: up one dir, main page]

Skip to content

Commit 7dc91de

Browse files
authored
Merge pull request #13720 from eric-wieser/more-with-statements
MAINT/BUG: Manage more files with with statements
2 parents 07c625c + b0399b9 commit 7dc91de

File tree

6 files changed

+63
-86
lines changed

6 files changed

+63
-86
lines changed

numpy/core/code_generators/genapi.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,11 @@ def get_versions_hash():
483483
d = []
484484

485485
file = os.path.join(os.path.dirname(__file__), 'cversions.txt')
486-
fid = open(file, 'r')
487-
try:
486+
with open(file, 'r') as fid:
488487
for line in fid:
489488
m = VERRE.match(line)
490489
if m:
491490
d.append((int(m.group(1), 16), m.group(2)))
492-
finally:
493-
fid.close()
494491

495492
return dict(d)
496493

numpy/core/records.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242

4343
from . import numeric as sb
4444
from . import numerictypes as nt
45-
from numpy.compat import isfileobj, bytes, long, unicode, os_fspath
45+
from numpy.compat import (
46+
isfileobj, bytes, long, unicode, os_fspath, contextlib_nullcontext
47+
)
4648
from numpy.core.overrides import set_module
4749
from .arrayprint import get_printoptions
4850

@@ -777,44 +779,42 @@ def fromfile(fd, dtype=None, shape=None, offset=0, formats=None,
777779

778780
if isfileobj(fd):
779781
# file already opened
780-
name = 0
782+
ctx = contextlib_nullcontext(fd)
781783
else:
782784
# open file
783-
fd = open(os_fspath(fd), 'rb')
784-
name = 1
785+
ctx = open(os_fspath(fd), 'rb')
785786

786-
if (offset > 0):
787-
fd.seek(offset, 1)
788-
size = get_remaining_size(fd)
787+
with ctx as fd:
788+
if (offset > 0):
789+
fd.seek(offset, 1)
790+
size = get_remaining_size(fd)
789791

790-
if dtype is not None:
791-
descr = sb.dtype(dtype)
792-
else:
793-
descr = format_parser(formats, names, titles, aligned, byteorder)._descr
792+
if dtype is not None:
793+
descr = sb.dtype(dtype)
794+
else:
795+
descr = format_parser(formats, names, titles, aligned, byteorder)._descr
794796

795-
itemsize = descr.itemsize
797+
itemsize = descr.itemsize
796798

797-
shapeprod = sb.array(shape).prod(dtype=nt.intp)
798-
shapesize = shapeprod * itemsize
799-
if shapesize < 0:
800-
shape = list(shape)
801-
shape[shape.index(-1)] = size // -shapesize
802-
shape = tuple(shape)
803799
shapeprod = sb.array(shape).prod(dtype=nt.intp)
800+
shapesize = shapeprod * itemsize
801+
if shapesize < 0:
802+
shape = list(shape)
803+
shape[shape.index(-1)] = size // -shapesize
804+
shape = tuple(shape)
805+
shapeprod = sb.array(shape).prod(dtype=nt.intp)
804806

805-
nbytes = shapeprod * itemsize
807+
nbytes = shapeprod * itemsize
806808

807-
if nbytes > size:
808-
raise ValueError(
809-
"Not enough bytes left in file for specified shape and type")
809+
if nbytes > size:
810+
raise ValueError(
811+
"Not enough bytes left in file for specified shape and type")
810812

811-
# create the array
812-
_array = recarray(shape, descr)
813-
nbytesread = fd.readinto(_array.data)
814-
if nbytesread != nbytes:
815-
raise IOError("Didn't read as many bytes as expected")
816-
if name:
817-
fd.close()
813+
# create the array
814+
_array = recarray(shape, descr)
815+
nbytesread = fd.readinto(_array.data)
816+
if nbytesread != nbytes:
817+
raise IOError("Didn't read as many bytes as expected")
818818

819819
return _array
820820

numpy/core/setup_common.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -312,30 +312,24 @@ def pyod(filename):
312312
def _pyod2():
313313
out = []
314314

315-
fid = open(filename, 'rb')
316-
try:
315+
with open(filename, 'rb') as fid:
317316
yo = [int(oct(int(binascii.b2a_hex(o), 16))) for o in fid.read()]
318-
for i in range(0, len(yo), 16):
319-
line = ['%07d' % int(oct(i))]
320-
line.extend(['%03d' % c for c in yo[i:i+16]])
321-
out.append(" ".join(line))
322-
return out
323-
finally:
324-
fid.close()
317+
for i in range(0, len(yo), 16):
318+
line = ['%07d' % int(oct(i))]
319+
line.extend(['%03d' % c for c in yo[i:i+16]])
320+
out.append(" ".join(line))
321+
return out
325322

326323
def _pyod3():
327324
out = []
328325

329-
fid = open(filename, 'rb')
330-
try:
326+
with open(filename, 'rb') as fid:
331327
yo2 = [oct(o)[2:] for o in fid.read()]
332-
for i in range(0, len(yo2), 16):
333-
line = ['%07d' % int(oct(i)[2:])]
334-
line.extend(['%03d' % int(c) for c in yo2[i:i+16]])
335-
out.append(" ".join(line))
336-
return out
337-
finally:
338-
fid.close()
328+
for i in range(0, len(yo2), 16):
329+
line = ['%07d' % int(oct(i)[2:])]
330+
line.extend(['%03d' % int(c) for c in yo2[i:i+16]])
331+
out.append(" ".join(line))
332+
return out
339333

340334
if sys.version_info[0] < 3:
341335
return _pyod2()

numpy/distutils/system_info.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -301,26 +301,21 @@ def add_system_root(library_root):
301301
default_x11_include_dirs.extend(['/usr/lib/X11/include',
302302
'/usr/include/X11'])
303303

304-
tmp = None
305-
try:
306-
# Explicitly open/close file to avoid ResourceWarning when
307-
# tests are run in debug mode Python 3.
308-
tmp = open(os.devnull, 'w')
309-
p = subprocess.Popen(["gcc", "-print-multiarch"], stdout=subprocess.PIPE,
310-
stderr=tmp)
311-
except (OSError, DistutilsError):
312-
# OSError if gcc is not installed, or SandboxViolation (DistutilsError
313-
# subclass) if an old setuptools bug is triggered (see gh-3160).
314-
pass
315-
else:
316-
triplet = str(p.communicate()[0].decode().strip())
317-
if p.returncode == 0:
318-
# gcc supports the "-print-multiarch" option
319-
default_x11_lib_dirs += [os.path.join("/usr/lib/", triplet)]
320-
default_lib_dirs += [os.path.join("/usr/lib/", triplet)]
321-
finally:
322-
if tmp is not None:
323-
tmp.close()
304+
with open(os.devnull, 'w') as tmp:
305+
try:
306+
p = subprocess.Popen(["gcc", "-print-multiarch"], stdout=subprocess.PIPE,
307+
stderr=tmp)
308+
except (OSError, DistutilsError):
309+
# OSError if gcc is not installed, or SandboxViolation (DistutilsError
310+
# subclass) if an old setuptools bug is triggered (see gh-3160).
311+
pass
312+
else:
313+
triplet = str(p.communicate()[0].decode().strip())
314+
if p.returncode == 0:
315+
# gcc supports the "-print-multiarch" option
316+
default_x11_lib_dirs += [os.path.join("/usr/lib/", triplet)]
317+
default_lib_dirs += [os.path.join("/usr/lib/", triplet)]
318+
324319

325320
if os.path.join(sys.prefix, 'lib') not in default_lib_dirs:
326321
default_lib_dirs.insert(0, os.path.join(sys.prefix, 'lib'))

numpy/lib/_datasource.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import warnings
4242
import shutil
4343
import io
44+
from contextlib import closing
4445

4546
from numpy.core.overrides import set_module
4647

@@ -414,13 +415,9 @@ def _cache(self, path):
414415
# TODO: Doesn't handle compressed files!
415416
if self._isurl(path):
416417
try:
417-
openedurl = urlopen(path)
418-
f = _open(upath, 'wb')
419-
try:
420-
shutil.copyfileobj(openedurl, f)
421-
finally:
422-
f.close()
423-
openedurl.close()
418+
with closing(urlopen(path)) as openedurl:
419+
with _open(upath, 'wb') as f:
420+
shutil.copyfileobj(openedurl, f)
424421
except URLError:
425422
raise URLError("URL not found: %s" % path)
426423
else:

numpy/lib/format.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -841,16 +841,12 @@ def open_memmap(filename, mode='r+', dtype=None, shape=None,
841841
shape=shape,
842842
)
843843
# If we got here, then it should be safe to create the file.
844-
fp = open(os_fspath(filename), mode+'b')
845-
try:
844+
with open(os_fspath(filename), mode+'b') as fp:
846845
_write_array_header(fp, d, version)
847846
offset = fp.tell()
848-
finally:
849-
fp.close()
850847
else:
851848
# Read the header of the file first.
852-
fp = open(os_fspath(filename), 'rb')
853-
try:
849+
with open(os_fspath(filename), 'rb') as fp:
854850
version = read_magic(fp)
855851
_check_version(version)
856852

@@ -859,8 +855,6 @@ def open_memmap(filename, mode='r+', dtype=None, shape=None,
859855
msg = "Array can't be memory-mapped: Python objects in dtype."
860856
raise ValueError(msg)
861857
offset = fp.tell()
862-
finally:
863-
fp.close()
864858

865859
if fortran_order:
866860
order = 'F'

0 commit comments

Comments
 (0)
0