From 8826e0ffc2c5286572dff1d490bcda88a6f7cd64 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Tue, 4 Jun 2019 23:05:08 -0700 Subject: MAINT: Use a with statement instead of try / finally All of this code was already correct, this just tidies it a little --- numpy/lib/format.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'numpy/lib/format.py') 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' -- cgit v1.2.1