TST: ensure datasets closed (#3444)

This commit is contained in:
Alan D. Snow 2025-12-05 09:42:40 -06:00 committed by GitHub
parent 13ae345efb
commit eaa3e6b45a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 32 deletions

View File

@ -228,18 +228,19 @@ def test_issue2360_no_with(monkeypatch, request, capfd, rgb_file_object):
monkeypatch.setattr(rasterio, "have_vsi_plugin", False)
with rasterio.Env() as env:
src = rasterio.open(rgb_file_object)
assert src.driver == "GTiff"
assert src.count == 3
assert src.dtypes == ("uint8", "uint8", "uint8")
assert src.read().shape == (3, 718, 791)
try:
assert src.driver == "GTiff"
assert src.count == 3
assert src.dtypes == ("uint8", "uint8", "uint8")
assert src.read().shape == (3, 718, 791)
env._dump_open_datasets()
captured = capfd.readouterr()
assert f"/vsimem/{request.node.name}" in captured.err
# Closing src causes the attached MemoryFile context to be
# exited and the temporary in-memory file is deleted.
src.close()
env._dump_open_datasets()
captured = capfd.readouterr()
assert f"/vsimem/{request.node.name}" in captured.err
finally:
# Closing src causes the attached MemoryFile context to be
# exited and the temporary in-memory file is deleted.
src.close()
env._dump_open_datasets()
captured = capfd.readouterr()
assert f"/vsimem/{request.node.name}" not in captured.err

View File

@ -1,6 +1,7 @@
"""Tests for ``rasterio.shutil```."""
from contextlib import ExitStack
import os
import numpy
@ -73,33 +74,33 @@ def test_copy(tmpdir, path_rgb_byte_tif, pass_handle):
outfile = str(tmpdir.join('test_copy.tif'))
if pass_handle:
src = rasterio.open(path_rgb_byte_tif)
else:
src = path_rgb_byte_tif
with ExitStack() as exit_stack:
if pass_handle:
src = exit_stack.enter_context(rasterio.open(path_rgb_byte_tif))
else:
src = path_rgb_byte_tif
rasterio.shutil.copy(
src,
outfile,
# Test a mix of boolean, ints, and strings to make sure creation
# options passed as Python types are properly cast.
tiled=True,
blockxsize=512,
BLOCKYSIZE='256')
rasterio.shutil.copy(
src,
outfile,
# Test a mix of boolean, ints, and strings to make sure creation
# options passed as Python types are properly cast.
tiled=True,
blockxsize=512,
BLOCKYSIZE='256')
if isinstance(src, str):
src = rasterio.open(path_rgb_byte_tif)
if isinstance(src, str):
src = exit_stack.enter_context(rasterio.open(path_rgb_byte_tif))
with rasterio.open(outfile) as dst:
assert dst.driver == 'GTiff'
assert set(dst.block_shapes) == {(256, 512)}
with rasterio.open(outfile) as dst:
assert dst.driver == 'GTiff'
assert set(dst.block_shapes) == {(256, 512)}
src_data = src.read()
dst_data = dst.read()
src_data = src.read()
dst_data = dst.read()
assert numpy.array_equal(src_data, dst_data)
assert numpy.array_equal(src_data, dst_data)
src.close()
def test_copy_bad_driver():