diff --git a/src/metpy/io/metar.py b/src/metpy/io/metar.py index 8ab8bb1c50e..844b922bd2b 100644 --- a/src/metpy/io/metar.py +++ b/src/metpy/io/metar.py @@ -4,6 +4,7 @@ """Parse METAR-formatted data.""" # Import the necessary libraries from collections import namedtuple +import contextlib from datetime import datetime import warnings @@ -521,11 +522,9 @@ def merge(x, key=' '): if len(tmp): yield ' '.join(tmp) - # Open the file - myfile = open_as_needed(filename, 'rt') - - # Clean up the file and take out the next line (\n) - value = myfile.read().rstrip() + # Open the file and clean up and take out the next line (\n) + with contextlib.closing(open_as_needed(filename, 'rt')) as myfile: + value = myfile.read().rstrip() list_values = value.split('\n') list_values = list(filter(None, list_values)) diff --git a/src/metpy/plots/cartopy_utils.py b/src/metpy/plots/cartopy_utils.py index 56bf4ab6088..b8ea6e1a9c1 100644 --- a/src/metpy/plots/cartopy_utils.py +++ b/src/metpy/plots/cartopy_utils.py @@ -27,7 +27,7 @@ def geometries(self): # Ensure that the associated files are in the cache fname = f'{self.name}_{self.scaler.scale}' for extension in ['.dbf', '.shx']: - get_test_data(fname + extension) + get_test_data(fname + extension, as_file_obj=False) path = get_test_data(fname + '.shp', as_file_obj=False) return iter(tuple(shapereader.Reader(path).geometries())) diff --git a/src/metpy/testing.py b/src/metpy/testing.py index 4f0cb3e66ae..2671d3ae9cf 100644 --- a/src/metpy/testing.py +++ b/src/metpy/testing.py @@ -7,6 +7,7 @@ * unit-aware test functions * code for testing matplotlib figures """ +import contextlib import functools import numpy as np @@ -57,8 +58,8 @@ def get_upper_air_data(date, station): '2002-11-11T00Z_BNA': 'nov11_sounding.txt', '2010-12-09T12Z_BOI': 'dec9_sounding.txt'} - fname = sounding_files[sounding_key] - fobj = get_test_data(fname) + # Initiate lists for variables + arr_data = [] def to_float(s): # Remove all whitespace and replace empty values with NaN @@ -66,21 +67,19 @@ def to_float(s): s = 'nan' return float(s) - # Skip dashes, column names, units, and more dashes - for _ in range(4): - fobj.readline() - - # Initiate lists for variables - arr_data = [] + with contextlib.closing(get_test_data(sounding_files[sounding_key])) as fobj: + # Skip dashes, column names, units, and more dashes + for _ in range(4): + fobj.readline() - # Read all lines of data and append to lists only if there is some data - for row in fobj: - level = to_float(row[0:7]) - values = (to_float(row[7:14]), to_float(row[14:21]), to_float(row[21:28]), - to_float(row[42:49]), to_float(row[49:56])) + # Read all lines of data and append to lists only if there is some data + for row in fobj: + level = to_float(row[0:7]) + values = (to_float(row[7:14]), to_float(row[14:21]), to_float(row[21:28]), + to_float(row[42:49]), to_float(row[49:56])) - if any(np.invert(np.isnan(values[1:]))): - arr_data.append((level,) + values) + if any(np.invert(np.isnan(values[1:]))): + arr_data.append((level,) + values) p, z, t, td, direc, spd = np.array(arr_data).T diff --git a/tests/io/test_nexrad.py b/tests/io/test_nexrad.py index 713763e5b0f..8a24ba2dad8 100644 --- a/tests/io/test_nexrad.py +++ b/tests/io/test_nexrad.py @@ -2,7 +2,7 @@ # Distributed under the terms of the BSD 3-Clause License. # SPDX-License-Identifier: BSD-3-Clause """Test the `nexrad` module.""" - +import contextlib from datetime import datetime from io import BytesIO import logging @@ -76,7 +76,8 @@ def read(self, n=None): def test_doubled_file(): """Test for #489 where doubled-up files didn't parse at all.""" - data = get_test_data('Level2_KFTG_20150430_1419.ar2v').read() + with contextlib.closing(get_test_data('Level2_KFTG_20150430_1419.ar2v')) as infile: + data = infile.read() fobj = BytesIO(data + data) f = Level2File(fobj) assert len(f.sweeps) == 12