Add test coverage for OSError handling in get_user() for Python 3.13+ compatibility #13836
+15
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Successfully addressed the Python 3.13+/Windows tmpdir crash issue. The code fix was already present in the main branch (added in commit 14d3707 and released in pytest 8.3.0), but was missing comprehensive test coverage.
Changes Made:
test_get_user_handles_oserror()intesting/test_tmpdir.pyReview Feedback Addressed:
@pytest.mark.skipif(not sys.platform.startswith("win"), reason="win only")monkeypatch.delenv()Test Details:
The test validates that when all username environment variables are removed on Windows,
get_user()returnsNoneinstead of crashing. On Python 3.13+, this exercises the OSError handling path. On older Python versions, it may use fallback mechanisms but should still return None.Original prompt
This section details on the original issue you should resolve
<issue_title>Pytest tmpdir crashes on Python 3.13+/Windows when no username env vars are set (getpass.getuser now raises OSError)</issue_title>
<issue_description>Bug report generated by co-pilot EXCEPT me having to edit a bunch since it was confusing and too much blabbing:
Summary:
On Python 3.13 and 3.14 (RC) under Windows, pytest fails early with
OSError: No username set in the environmentif none of LOGNAME, USER, LNAME, USERNAME are present. The failure originates in _pytest/tmpdir.get_user(), which only catches ImportError and KeyError, but Python 3.13 changed getpass.getuser() to raise OSError when no username can be derived. This was previously tolerated.Environment:
Relevant Python change:
Per Python 3.13 getpass.py (getuser docstring): Behavior changed to consistently raise OSError when no environment user identifiers exist and pwd (Unix-only) is not available.
Observed Error (truncated): OSError: No username set in the environment File .../Lib/getpass.py:236, in getuser raise OSError('No username set in the environment') from e
Pytest stack location: _in _pytest/tmpdir.py: get_user() calls getpass.getuser() without handling OSError.
Minimal Reproduction (Windows, Python 3.13+):
Create a clean directory with a trivial test file:
echo "def test_ok(): pass" > test_sample.pyLaunch Python with some variables cleared:
Execute:
python -c "import os, subprocess; [os.environ.pop(v,None) for v in ['LOGNAME','USER','LNAME','USERNAME']]; import pytest; raise SystemExit(pytest.main(['-q']))"Result:
pytest crashes with OSError before collection finishes.
Actual: Session aborts with uncaught OSError.
Root Cause:
Python 3.13 tighten semantics:
getpass.getuser() now raises OSError when no identifier found. Pytest’s get_user only catches ImportError and KeyError: except (ImportError, KeyError): return None Missing OSError handling lets the exception propagate.
Proposed Fix: Expand exception clause to include OSError.
Rationale:
Restores pre-3.13 resilient behavior.
Narrow catch still specific to getpass failure modes; avoids masking unrelated errors.
Workaround (for users now): Set an env var before running pytest
e.g. in conftest.py:
Or inject in tox.ini: [testenv] setenv = USERNAME = tester
Impact:
Any Windows/CI environment invoking pytest under Python 3.13+ without a username env var will fail until patched. Silent in earlier versions because getpass.getuser() returned a fallback or raised only ImportError/KeyError.
Regression Risk: Low. Adding OSError aligns with documented new behavior. Does not affect normal environments where env vars are present.</issue_description>
<agent_instructions>create the suggested minimal bugfix and accompany it with a minimal unittest and the requ...
Fixes #13835
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.