Skip to content

r.mfilter: Avoid reopening NamedTemporaryFile on Windows in testsuite - #7878

Merged
echoix merged 1 commit into
OSGeo:mainfrom
echoix:claude/grass-pr-2616-component-cgi1g2
Sep 3, 2026
Merged

echoix merged 1 commit into
OSGeo:mainfrom
echoix:claude/grass-pr-2616-component-cgi1g2

Conversation

@echoix

@echoix echoix commented Sep 2, 2026

Copy link
Copy Markdown
Member

Description

Extracts and reworks one self-contained change out of upstream PR
OSGeo/grass#2616 ("CI: Address
Windows specific issues in testsuite", open since 2022, 35 commits, by
Stefan Blumentrath / @ninsbl) so it can be evaluated on its own. Opened
against this fork, for testing, not upstream.

raster/r.mfilter/testsuite/test_r_mfilter.py creates its filter-definition
temp file with tempfile.NamedTemporaryFile() and keeps it open (via
.name) while r.mfilter opens the same path again. On Windows this second
open fails, because Windows does not allow reopening a file that another
handle (here, the still-open Python file object) has locked. That's why all
6 tests in this file carry @xfail_windows. #2616 replaces this with
grass.script.tempfile(create=False), which only reserves a unique path (via
g.tempfile -d) without holding it open, then writes to it directly.

Motivation and context

An AI (Claude) review of #2616 in July 2026, and a second one now, flagged
this as the easiest individual piece of that PR to land as its own, minimal
PR: it is confined to one test file, has a well-understood, verifiable root
cause (the double-open bug is exactly what @xfail_windows's own docstring
describes), and doesn't depend on any of the PR's other, more invasive
Windows changes.

That said, #2616's own diff for this file is not usable as a straight
cherry-pick: it was branched in 2022 and never rebased, so its diff against
current main also reverts changes unrelated to the tempfile fix that
landed on main in the meantime — a docstring correction
(nc_spm_full_v2alphav2 -> nc_spm_full_v2beta1), tightened variance
reference values, and trailing-whitespace cleanup. This PR re-implements
just the tempfile fix against current main, so none of that unrelated
history gets clobbered. It also drops filter.close() (no longer needed,
since filter is now a path string, not a file object) and the
@xfail_windows markers themselves, per the instruction in
xfail_windows's own docstring: "Once the test is fixed and passing, remove
the decorator."

I have not verified this on an actual Windows machine. If the Windows CI job
here still fails, the @xfail_windows markers should be restored, and the
underlying reasoning re-examined — the AI analysis should not be taken as
proof the fix works, only as a plausible, checkable hypothesis.

How has this been tested?

  • python3 -m py_compile on the changed file.
  • ruff check --preview and ruff format --diff (both clean, ruff 0.15.17
    to match .pre-commit-config.yaml).
  • Not run against a live GRASS build or on Windows (no build available in
    this environment) — relying on this fork's CI to confirm the fix on
    Windows and non-regression on Linux/macOS.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing
    functionality to not work as before)

Checklist

  • PR title provides summary of the changes and starts with one of the
    pre-defined prefixes
  • My code follows the code style
    of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes. (No new tests added; existing
    tests in this file are what the change fixes and un-xfails.)

Original fix idea from Stefan Blumentrath (@ninsbl) in
OSGeo/grass#2616, reworked here
against current main. AI-assisted (Claude) analysis and implementation;
please verify independently before relying on this, especially the removal
of the @xfail_windows markers.

Generated with Claude Code (sonnet 5)

On Windows, a file opened via tempfile.NamedTemporaryFile() cannot be
reopened by another process while the Python handle is still open, so
r.mfilter failed to read the filter file created this way. Write the
filter contents to a path from grass.script.tempfile() instead, which
does not hold the file open, and drop the @xfail_windows markers this
fix should resolve.

Extracted and reworked as a standalone change from PR OSGeo#2616 (by Stefan
Blumentrath / @ninsbl), which combined this fix with many other
Windows-specific testsuite changes and has been open, unrebased, since
2022. Re-derived against current main rather than cherry-picked,
because @ninsbl's branch predates unrelated fixes already merged to
this same test file (a docstring/variance correction and other
@xfail_windows additions), which a direct cherry-pick would have
clobbered.

This has not been verified on an actual Windows run; CI on this PR
should confirm whether the @xfail_windows markers can indeed be
removed. Analysis of PR OSGeo#2616 assisted by AI (Claude), including
identifying this as an easily isolated, self-contained candidate for
its own PR; verify before merging.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@github-actions github-actions Bot added raster Related to raster data processing Python Related code is in Python module tests Related to Test Suite labels Sep 2, 2026
@echoix

echoix commented Sep 2, 2026

Copy link
Copy Markdown
Member Author

On my fork, the tests of this PR passed on windows, 351 files passing, 8 failed. The r.mfilter test passes.

@echoix
echoix requested a review from ninsbl September 3, 2026 01:49

@ninsbl ninsbl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@echoix
echoix merged commit 5734e23 into OSGeo:main Sep 3, 2026
26 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Sep 3, 2026
echoix added a commit to echoix/grass that referenced this pull request Sep 6, 2026
test_file kept a NamedTemporaryFile handle open while passing its path
to r.kappa as the output file. Windows rejects a second process
opening a file while the creating process still holds the handle;
POSIX allows it. Use grass.script.core.tempfile() to get an unopened
path instead, the same fix already applied to r.mfilter's testsuite in
OSGeo#7878, and drop the now-unneeded @xfail_windows marker.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
echoix added a commit to echoix/grass that referenced this pull request Sep 6, 2026
test_file kept a NamedTemporaryFile handle open while passing its path
to r.kappa as the output file. Windows rejects a second process
opening a file while the creating process still holds the handle;
POSIX allows it. Use grass.script.core.tempfile() to get an unopened
path instead, the same fix already applied to r.mfilter's testsuite in
OSGeo#7878, and drop the now-unneeded @xfail_windows marker.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@echoix
echoix deleted the claude/grass-pr-2616-component-cgi1g2 branch September 12, 2026 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module Python Related code is in Python raster Related to raster data processing tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants