@@ -349,21 +349,27 @@ def _clean_location(self) -> bool:
349
349
shutil .rmtree (self .location )
350
350
return True
351
351
352
+ def _read_pyvenv_cfg (self ) -> dict [str , str ] | None :
353
+ """Read a pyvenv.cfg file into dict, returns None if missing."""
354
+ path = os .path .join (self .location , "pyvenv.cfg" )
355
+ with contextlib .suppress (FileNotFoundError ), open (path ) as fp :
356
+ parts = (x .partition ("=" ) for x in fp if "=" in x )
357
+ return {k .strip (): v .strip () for k , _ , v in parts }
358
+ return None
359
+
352
360
def _check_reused_environment_type (self ) -> bool :
353
361
"""Check if reused environment type is the same or equivalent."""
354
- try :
355
- with open (os .path .join (self .location , "pyvenv.cfg" )) as fp :
356
- parts = (x .partition ("=" ) for x in fp if "=" in x )
357
- config = {k .strip (): v .strip () for k , _ , v in parts }
358
- if "uv" in config or "gourgeist" in config :
359
- old_env = "uv"
360
- elif "virtualenv" in config :
361
- old_env = "virtualenv"
362
- else :
363
- old_env = "venv"
364
- except FileNotFoundError : # pragma: no cover
365
- # virtualenv < 20.0 does not create pyvenv.cfg
362
+
363
+ config = self ._read_pyvenv_cfg ()
364
+ # virtualenv < 20.0 does not create pyvenv.cfg
365
+ if config is None :
366
366
old_env = "virtualenv"
367
+ elif "uv" in config or "gourgeist" in config :
368
+ old_env = "uv"
369
+ elif "virtualenv" in config :
370
+ old_env = "virtualenv"
371
+ else :
372
+ old_env = "venv"
367
373
368
374
# Can't detect mamba separately, but shouldn't matter
369
375
if os .path .isdir (os .path .join (self .location , "conda-meta" )):
@@ -395,7 +401,9 @@ def _check_reused_environment_interpreter(self) -> bool:
395
401
if not os .environ .get ("NOX_ENABLE_STALENESS_CHECK" , "" ):
396
402
return True
397
403
398
- original = self ._read_base_prefix_from_pyvenv_cfg ()
404
+ config = self ._read_pyvenv_cfg () or {}
405
+ original = config .get ("base-prefix" , None )
406
+
399
407
program = (
400
408
"import sys; sys.stdout.write(getattr(sys, 'real_prefix', sys.base_prefix))"
401
409
)
@@ -417,17 +425,6 @@ def _check_reused_environment_interpreter(self) -> bool:
417
425
and os .path .samefile (original , created )
418
426
)
419
427
420
- def _read_base_prefix_from_pyvenv_cfg (self ) -> str | None :
421
- """Return the base-prefix entry from pyvenv.cfg, if present."""
422
- path = os .path .join (self .location , "pyvenv.cfg" )
423
- if os .path .isfile (path ):
424
- with open (path ) as io :
425
- for line in io :
426
- key , _ , value = line .partition ("=" )
427
- if key .strip () == "base-prefix" :
428
- return value .strip ()
429
- return None
430
-
431
428
@property
432
429
def _resolved_interpreter (self ) -> str :
433
430
"""Return the interpreter, appropriately resolved for the platform.
0 commit comments