@@ -796,7 +796,7 @@ def venv(known_paths):
796796 if candidate_conf :
797797 virtual_conf = candidate_conf
798798 system_site = "true"
799- version = None
799+ version , version_info = None , None
800800 # Issue 25185: Use UTF-8, as that's what the venv module uses when
801801 # writing the file.
802802 with open (virtual_conf , encoding = 'utf-8' ) as f :
@@ -811,25 +811,33 @@ def venv(known_paths):
811811 sys ._home = value
812812 elif key == 'version' :
813813 version = value
814-
815- if version :
816- try :
817- major , minor = map (int , version .split ("." )[:2 ])
818- except ValueError :
819- major , minor = None
820- if (
821- major == sys .version_info .major
822- and minor is not None
823- and minor != sys .version_info .minor
824- and not hasattr (sys , "_venv_version_warning_emitted" )
825- ):
826- _warn (
827- f"This virtual environment was created for Python { major } .{ minor } , "
828- f"but the current interpreter is Python "
829- f"{ sys .version_info .major } .{ sys .version_info .minor } . "
830- "Consider running `python -m venv --upgrade` to update the environment." ,
831- RuntimeWarning ,
832- )
814+ elif key == 'version_info' :
815+ version_info = value
816+
817+ for field_name , field_value in [
818+ ('version' ,version ), ('version_info' ,version_info )
819+ ]:
820+ if field_value is not None :
821+ try :
822+ major , minor = map (int , field_value .split ("." )[:2 ])
823+ except (ValueError , AttributeError ):
824+ _warn (
825+ f"Malformed { field_name } string in pyvenv.cfg: { field_value !r} " ,
826+ RuntimeWarning ,
827+ )
828+ else :
829+ if (
830+ major == sys .version_info .major
831+ and minor != sys .version_info .minor
832+ ):
833+ _warn (
834+ f"This virtual environment was created for Python { major } .{ minor } , "
835+ f"but the current interpreter is Python "
836+ f"{ sys .version_info .major } .{ sys .version_info .minor } . "
837+ "Consider running `python -m venv --upgrade` to update the environment." ,
838+ RuntimeWarning ,
839+ )
840+ break
833841
834842 if sys .prefix != site_prefix :
835843 _warn (f'Unexpected value in sys.prefix, expected { site_prefix } , got { sys .prefix } ' , RuntimeWarning )
0 commit comments