Skip to content

Commit 1a1e4b1

Browse files
committed
Use tmp_path in test_nodata_out_of_range_1581 to fix Windows file lock
The tests in test_nodata_out_of_range_1581.py wrote via tempfile.NamedTemporaryFile(delete=False) and unlinked manually at teardown. On Windows that fails with PermissionError [WinError 32]: open_geotiff and read_geotiff_dask leave the file's mmap alive in the reader's LRU cache, and Windows refuses to delete an mapped file. tmp_path leaves cleanup to pytest, which tolerates locked files at session teardown.
1 parent e7c3416 commit 1a1e4b1

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

xrspatial/geotiff/tests/test_nodata_out_of_range_1581.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
from __future__ import annotations
1313

1414
import importlib.util
15-
import os
16-
import tempfile
1715

1816
import numpy as np
1917
import pytest
@@ -49,14 +47,18 @@ def _gpu_available() -> bool:
4947

5048

5149
@pytest.fixture
52-
def uint16_neg_nodata_tif():
50+
def uint16_neg_nodata_tif(tmp_path):
51+
"""Write a uint16 TIFF with an out-of-range negative nodata sentinel.
52+
53+
Uses pytest's ``tmp_path`` rather than ``tempfile.NamedTemporaryFile``
54+
so the mmap cache in ``_reader.py`` does not block teardown on Windows
55+
(open mmaps cannot be unlinked there).
56+
"""
5357
arr = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint16)
5458
da = xr.DataArray(arr, dims=['y', 'x'])
55-
with tempfile.NamedTemporaryFile(suffix='.tif', delete=False) as f:
56-
path = f.name
59+
path = str(tmp_path / 'uint16_neg_nodata.tif')
5760
to_geotiff(da, path, crs=4326, nodata=-9999)
5861
yield path, arr
59-
os.unlink(path)
6062

6163

6264
def test_int_nodata_in_range_helper():
@@ -118,23 +120,19 @@ def test_read_geotiff_dask_uint16_negative_nodata_compute(
118120
np.testing.assert_array_equal(result.values, expected)
119121

120122

121-
def test_open_geotiff_uint16_in_range_nodata_still_masks():
123+
def test_open_geotiff_uint16_in_range_nodata_still_masks(tmp_path):
122124
"""The fix doesn't regress the in-range case: uint16 + nodata=65535
123125
still promotes to float64 and masks to NaN."""
124126
arr = np.array([[1, 2, 3], [4, 5, 65535]], dtype=np.uint16)
125127
da = xr.DataArray(arr, dims=['y', 'x'])
126-
with tempfile.NamedTemporaryFile(suffix='.tif', delete=False) as f:
127-
path = f.name
128-
try:
129-
to_geotiff(da, path, crs=4326, nodata=65535)
130-
result = open_geotiff(path)
131-
assert result.dtype == np.float64
132-
# The 65535 pixel should be NaN; the rest unchanged.
133-
assert np.isnan(result.values[1, 2])
134-
assert result.values[0, 0] == 1
135-
assert result.attrs.get('nodata') == 65535.0
136-
finally:
137-
os.unlink(path)
128+
path = str(tmp_path / 'uint16_in_range_nodata.tif')
129+
to_geotiff(da, path, crs=4326, nodata=65535)
130+
result = open_geotiff(path)
131+
assert result.dtype == np.float64
132+
# The 65535 pixel should be NaN; the rest unchanged.
133+
assert np.isnan(result.values[1, 2])
134+
assert result.values[0, 0] == 1
135+
assert result.attrs.get('nodata') == 65535.0
138136

139137

140138
@_gpu_only

0 commit comments

Comments
 (0)