Skip to content

Commit f2553cb

Browse files
RonnyPfannschmidtCursor AIAnthropic Claude Opus 4
committed
fix: address review comments for nodeid prefix computation
- Handle AttributeError for site.getsitepackages() and site.getusersitepackages() which may not exist in some virtualenv configurations - Add explanatory comments for bare OSError exception handlers - Fix Windows compatibility in test by normalizing path separators Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Anthropic Claude Opus 4 <claude@anthropic.com>
1 parent 7bbe39c commit f2553cb

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/_pytest/nodes.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,17 +578,28 @@ def _get_site_packages_dirs() -> frozenset[Path]:
578578

579579
dirs: set[Path] = set()
580580
# Standard site-packages
581-
for sp in site.getsitepackages():
581+
# getsitepackages() may not exist in some virtualenv configurations
582+
try:
583+
site_packages = site.getsitepackages()
584+
except AttributeError:
585+
site_packages = []
586+
for sp in site_packages:
582587
try:
583588
dirs.add(Path(sp).resolve())
584589
except OSError:
590+
# Path resolution can fail (deleted dir, permission issues, etc.)
585591
pass
586592
# User site-packages
587-
user_site = site.getusersitepackages()
593+
# getusersitepackages() may not exist in some virtualenv configurations
594+
try:
595+
user_site = site.getusersitepackages()
596+
except AttributeError:
597+
user_site = None
588598
if user_site:
589599
try:
590600
dirs.add(Path(user_site).resolve())
591601
except OSError:
602+
# Path resolution can fail (deleted dir, permission issues, etc.)
592603
pass
593604
return frozenset(dirs)
594605

testing/test_nodes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# mypy: allow-untyped-defs
22
from __future__ import annotations
33

4+
import os
45
from pathlib import Path
56
import re
67
import warnings
@@ -289,7 +290,8 @@ def test_compute_nodeid_far_away_absolute(self, tmp_path: Path) -> None:
289290
)
290291

291292
# Should use absolute path since it's more than 2 levels up
292-
assert result == str(far_away)
293+
# Use nodes.SEP for cross-platform compatibility (nodeids always use forward slashes)
294+
assert result == str(far_away).replace(os.sep, nodes.SEP)
293295

294296
def test_compute_nodeid_rootpath_itself(self, tmp_path: Path) -> None:
295297
"""Test nodeid computation for rootpath itself returns empty string."""

0 commit comments

Comments
 (0)