From dddd6d4252aa4068e7050edf897cf1927c22316c Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 9 Jun 2023 13:33:50 -0400 Subject: [PATCH 1/2] close files in tests and before raising exception in get_open_msa_slits --- jwst/assign_wcs/nirspec.py | 1 + jwst/assign_wcs/tests/test_nirspec.py | 18 +++++++++--------- jwst/associations/tests/test_mkpool.py | 9 +++++---- .../tests/test_outlier_detection.py | 3 +++ .../tests/test_background_fit.py | 16 ++++++++-------- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/jwst/assign_wcs/nirspec.py b/jwst/assign_wcs/nirspec.py index 679627dd78..0e1eee0dcd 100644 --- a/jwst/assign_wcs/nirspec.py +++ b/jwst/assign_wcs/nirspec.py @@ -674,6 +674,7 @@ def get_open_msa_slits(msa_file, msa_metadata_id, dither_position, log.warning(message) message = ("MSA configuration file has more than 1 shutter with primary source") log.warning(message) + msa_file.close() raise MSAFileError(message) # subtract 1 because shutter numbers in the MSA reference file are 1-based. diff --git a/jwst/assign_wcs/tests/test_nirspec.py b/jwst/assign_wcs/tests/test_nirspec.py index db898e7d85..e9a34badf7 100644 --- a/jwst/assign_wcs/tests/test_nirspec.py +++ b/jwst/assign_wcs/tests/test_nirspec.py @@ -166,17 +166,17 @@ def test_nirspec_ifu_against_esa(wcs_ifu_grating): """ Test Nirspec IFU mode using CV3 reference files. """ - ref = fits.open(get_file_path('Trace_IFU_Slice_00_SMOS-MOD-G1M-17-5344175105_30192_JLAB88.fits')) + with fits.open(get_file_path('Trace_IFU_Slice_00_SMOS-MOD-G1M-17-5344175105_30192_JLAB88.fits')) as ref: + # Test NRS1 + pyw = astwcs.WCS(ref['SLITY1'].header) + # Test evaluating the WCS (slice 0) + im, refs = wcs_ifu_grating("G140M", "OPAQUE") + w0 = nirspec.nrs_wcs_set_input(im, 0) - # Test NRS1 - pyw = astwcs.WCS(ref['SLITY1'].header) - # Test evaluating the WCS (slice 0) - im, refs = wcs_ifu_grating("G140M", "OPAQUE") - w0 = nirspec.nrs_wcs_set_input(im, 0) + # get positions within the slit and the corresponding lambda + slit1 = ref['SLITY1'].data # y offset on the slit + lam = ref['LAMBDA1'].data - # get positions within the slit and the corresponding lambda - slit1 = ref['SLITY1'].data # y offset on the slit - lam = ref['LAMBDA1'].data # filter out locations outside the slit cond = np.logical_and(slit1 < .5, slit1 > -.5) y, x = cond.nonzero() # 0-based diff --git a/jwst/associations/tests/test_mkpool.py b/jwst/associations/tests/test_mkpool.py index 12ac10829f..563c082589 100644 --- a/jwst/associations/tests/test_mkpool.py +++ b/jwst/associations/tests/test_mkpool.py @@ -22,10 +22,10 @@ def test_hdu(exposures): - hdus = [ - fits.open(exposure)[0] - for exposure in exposures - ] + hdus = [] + for exposure in exposures: + with fits.open(exposure) as hdulist: + hdus.append(hdulist[0]) pool = mkpool(hdus) assert isinstance(pool, AssociationPool) assert REQUIRED_PARAMS.issubset(pool.colnames) @@ -41,6 +41,7 @@ def test_hdulist(exposures): assert isinstance(pool, AssociationPool) assert REQUIRED_PARAMS.issubset(pool.colnames) assert len(pool) == len(exposures) + [h.close() for h in hduls] def test_mkpool(exposures): diff --git a/jwst/outlier_detection/tests/test_outlier_detection.py b/jwst/outlier_detection/tests/test_outlier_detection.py index e167ea38c9..076655f20d 100644 --- a/jwst/outlier_detection/tests/test_outlier_detection.py +++ b/jwst/outlier_detection/tests/test_outlier_detection.py @@ -251,6 +251,9 @@ def test_outlier_step_square_source_no_outliers(we_three_sci, _jail): np.testing.assert_allclose(image.data, corrected.data) np.testing.assert_allclose(image.dq, corrected.dq) + container.close() + pristine.close() + @pytest.mark.parametrize("exptype", IMAGE_MODES) def test_outlier_step_image_weak_CR_dither(exptype, _jail): diff --git a/jwst/residual_fringe/tests/test_background_fit.py b/jwst/residual_fringe/tests/test_background_fit.py index 3ec457d70d..a235ae9b31 100644 --- a/jwst/residual_fringe/tests/test_background_fit.py +++ b/jwst/residual_fringe/tests/test_background_fit.py @@ -19,14 +19,14 @@ def read_fit_column(file): file_dir = Path(__file__).parent.resolve() file_path = str(file_dir / file) - hdu = fits.open(file_path) - col_data = hdu[1].data - col_weight = hdu[2].data - col_wnum = hdu[3].data - bg_fit = hdu[4].data - store_freq = hdu[0].header['FFREQ'] - - return col_data, col_weight, col_wnum, bg_fit, store_freq + with fits.open(file_path) as hdu: + col_data = hdu[1].data + col_weight = hdu[2].data + col_wnum = hdu[3].data + bg_fit = hdu[4].data + store_freq = hdu[0].header['FFREQ'] + + return col_data, col_weight, col_wnum, bg_fit, store_freq @pytest.mark.parametrize("file", ['good_col.fits', 'edge_col.fits']) From 78948f7360707b53f79bdc0d9b47ca74b9921037 Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 9 Jun 2023 13:46:00 -0400 Subject: [PATCH 2/2] update changes --- CHANGES.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 2db9e13303..e67339ede9 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -71,6 +71,8 @@ other - Override package dependencies with requirements file when requested [#7557] +- Close files left open in test suite [#7599] + pathloss --------