Skip to content

Commit bf6470d

Browse files
committed
Optional arguments MinVersion and MaxVersion were added to the GetRegisteredPythonVersion routines.
1 parent c1f9907 commit bf6470d

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

Source/PythonVersions.pas

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ TPythonVersion = record
5757
The function result has the semantics of Delphi compare functions
5858
-1: A is bigger (newer), 0: equal versions, 1: B is bigger (newer)
5959
*)
60-
function CompareVersions(A, B : string) : Integer;
61-
60+
function CompareVersions(A, B : string) : Integer;
6261

6362
{$IFDEF MSWINDOWS}
6463
(* Checks whether an executable was compiled for X64 *)
@@ -69,11 +68,14 @@ TPythonVersion = record
6968
function GetRegisteredPythonVersion(SysVersion: string;
7069
out PythonVersion: TPythonVersion): Boolean;
7170
(* Returns all registered Python versions *)
72-
function GetRegisteredPythonVersions : TPythonVersions;
71+
function GetRegisteredPythonVersions(const MinVersion: string = '0.0';
72+
const MaxVersion: string = '100.100'): TPythonVersions;
7373
(* Returns the highest numbered registered Python version *)
74-
function GetLatestRegisteredPythonVersion(out PythonVersion: TPythonVersion): Boolean;
74+
function GetLatestRegisteredPythonVersion(out PythonVersion: TPythonVersion;
75+
const MinVersion: string = '0.0'; const MaxVersion: string = '100.100'): Boolean;
7576
function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVersion;
76-
AcceptVirtualEnvs: Boolean = True): Boolean;
77+
AcceptVirtualEnvs: Boolean = True; const MinVersion: string = '0.0';
78+
const MaxVersion: string = '100.100'): Boolean;
7779
{$ENDIF}
7880

7981
implementation
@@ -402,7 +404,8 @@ function GetRegisteredPythonVersion(SysVersion: string;
402404
PythonVersion.IsRegistered := Result;
403405
end;
404406

405-
function GetRegisteredPythonVersions : TPythonVersions;
407+
function GetRegisteredPythonVersions(const MinVersion: string = '0.0';
408+
const MaxVersion: string = '100.100'): TPythonVersions;
406409
Var
407410
Count: Integer;
408411
I: Integer;
@@ -411,27 +414,40 @@ function GetRegisteredPythonVersions : TPythonVersions;
411414
Count := 0;
412415
SetLength(Result, High(PYTHON_KNOWN_VERSIONS));
413416
for I := High(PYTHON_KNOWN_VERSIONS) downto 1 do
417+
begin
418+
if CompareVersions(PYTHON_KNOWN_VERSIONS[I].RegVersion, MaxVersion) < 0 then
419+
continue;
420+
if CompareVersions(PYTHON_KNOWN_VERSIONS[I].RegVersion, MinVersion) > 0 then
421+
break;
414422
if GetRegisteredPythonVersion(PYTHON_KNOWN_VERSIONS[I].RegVersion, PythonVersion) then
415423
begin
416424
Result[Count] := PythonVersion;
417425
Inc(Count);
418426
end;
427+
end;
419428
SetLength(Result, Count);
420429
end;
421430

422-
function GetLatestRegisteredPythonVersion(out PythonVersion: TPythonVersion): Boolean;
431+
function GetLatestRegisteredPythonVersion(out PythonVersion: TPythonVersion;
432+
const MinVersion: string = '0.0'; const MaxVersion: string = '100.100'): Boolean;
423433
Var
424434
I: Integer;
425435
begin
436+
Result := False;
426437
for I := High(PYTHON_KNOWN_VERSIONS) downto 1 do
427438
begin
428-
Result := GetRegisteredPythonVersion(PYTHON_KNOWN_VERSIONS[I].RegVersion, PythonVersion);
429-
if Result then break;
439+
if CompareVersions(PYTHON_KNOWN_VERSIONS[I].RegVersion, MaxVersion) < 0 then
440+
continue;
441+
if CompareVersions(PYTHON_KNOWN_VERSIONS[I].RegVersion, MinVersion) > 0 then
442+
break;
443+
if GetRegisteredPythonVersion(PYTHON_KNOWN_VERSIONS[I].RegVersion, PythonVersion) then
444+
Exit(True);
430445
end;
431446
end;
432447

433448
function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVersion;
434-
AcceptVirtualEnvs: Boolean = True): Boolean;
449+
AcceptVirtualEnvs: Boolean = True; const MinVersion: string = '0.0';
450+
const MaxVersion: string = '100.100'): Boolean;
435451

436452
function FindPythonDLL(APath : string): string;
437453
Var
@@ -513,11 +529,15 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
513529
PythonVersion.SysVersion := SysVersion;
514530
PythonVersion.fSysArchitecture := PythonVersion.ExpectedArchitecture;
515531

516-
for I := High(PYTHON_KNOWN_VERSIONS) downto 1 do
517-
if PYTHON_KNOWN_VERSIONS[I].RegVersion = SysVersion then begin
518-
Result := True;
519-
break;
520-
end;
532+
if (CompareVersions(MinVersion, SysVersion) >= 0) and
533+
(CompareVersions(MaxVersion, SysVersion) <= 0)
534+
then
535+
// Full search in case some python version is not supported
536+
for I := High(PYTHON_KNOWN_VERSIONS) downto 1 do
537+
if PYTHON_KNOWN_VERSIONS[I].RegVersion = SysVersion then begin
538+
Result := True;
539+
break;
540+
end;
521541
end;
522542

523543
{$ENDIF}

0 commit comments

Comments
 (0)