Skip to content

Commit

Permalink
MNT: Be more robust about closing files we've opened
Browse files Browse the repository at this point in the history
Inspired by some alerts from CodeQL, though most were false alarms.
  • Loading branch information
dopplershift committed Jun 30, 2021
1 parent 28d95a2 commit e5c47a5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
9 changes: 4 additions & 5 deletions src/metpy/io/metar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""Parse METAR-formatted data."""
# Import the necessary libraries
from collections import namedtuple
import contextlib
from datetime import datetime
import warnings

Expand Down Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion src/metpy/plots/cartopy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down
29 changes: 14 additions & 15 deletions src/metpy/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* unit-aware test functions
* code for testing matplotlib figures
"""
import contextlib
import functools

import numpy as np
Expand Down Expand Up @@ -57,30 +58,28 @@ 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
if not s.strip():
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

Expand Down
5 changes: 3 additions & 2 deletions tests/io/test_nexrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e5c47a5

Please sign in to comment.