Skip to content

geotiff: make _FileSource a context manager (cleanup follow-up to #2444) #2449

Closed

Description

@brendancol

Reason or Problem

xrspatial.geotiff._sources._FileSource exposes close() but no __enter__ / __exit__. Every call site has to wrap it in try / finally to guarantee mmap release. PR #2448 added another such call site in _vrt_validation._check_mixed_source_crs, and a code-review nit flagged the resulting try / except / finally / continue shape as harder to scan than a with block.

Proposal

Add __enter__ / __exit__ to _FileSource so callers can write:

with _FileSource(path) as src:
    data = src.read_all()
    ...

Then sweep the existing call sites (search for _FileSource( in xrspatial/geotiff/) and replace each try / finally with a with block. The mmap-cache release semantics stay the same; the helper change is mechanical.

Drawbacks

Mechanical sweep across several files. Worth verifying that no call site relies on the handle outliving a function scope before swapping wholesale.

Additional Notes or Context

Surfaced during review of PR #2448. Not blocking that PR; tracked here so the cleanup is not forgotten.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgeotiffGeoTIFF module

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions