@@ -57,8 +57,7 @@ TPythonVersion = record
57
57
The function result has the semantics of Delphi compare functions
58
58
-1: A is bigger (newer), 0: equal versions, 1: B is bigger (newer)
59
59
*)
60
- function CompareVersions (A, B : string) : Integer;
61
-
60
+ function CompareVersions (A, B : string) : Integer;
62
61
63
62
{ $IFDEF MSWINDOWS}
64
63
(* Checks whether an executable was compiled for X64 *)
@@ -69,11 +68,14 @@ TPythonVersion = record
69
68
function GetRegisteredPythonVersion (SysVersion: string;
70
69
out PythonVersion: TPythonVersion): Boolean;
71
70
(* Returns all registered Python versions *)
72
- function GetRegisteredPythonVersions : TPythonVersions;
71
+ function GetRegisteredPythonVersions (const MinVersion: string = ' 0.0' ;
72
+ const MaxVersion: string = ' 100.100' ): TPythonVersions;
73
73
(* 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;
75
76
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;
77
79
{ $ENDIF}
78
80
79
81
implementation
@@ -402,7 +404,8 @@ function GetRegisteredPythonVersion(SysVersion: string;
402
404
PythonVersion.IsRegistered := Result;
403
405
end ;
404
406
405
- function GetRegisteredPythonVersions : TPythonVersions;
407
+ function GetRegisteredPythonVersions (const MinVersion: string = ' 0.0' ;
408
+ const MaxVersion: string = ' 100.100' ): TPythonVersions;
406
409
Var
407
410
Count: Integer;
408
411
I: Integer;
@@ -411,27 +414,40 @@ function GetRegisteredPythonVersions : TPythonVersions;
411
414
Count := 0 ;
412
415
SetLength(Result, High(PYTHON_KNOWN_VERSIONS));
413
416
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;
414
422
if GetRegisteredPythonVersion(PYTHON_KNOWN_VERSIONS[I].RegVersion, PythonVersion) then
415
423
begin
416
424
Result[Count] := PythonVersion;
417
425
Inc(Count);
418
426
end ;
427
+ end ;
419
428
SetLength(Result, Count);
420
429
end ;
421
430
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;
423
433
Var
424
434
I: Integer;
425
435
begin
436
+ Result := False;
426
437
for I := High(PYTHON_KNOWN_VERSIONS) downto 1 do
427
438
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);
430
445
end ;
431
446
end ;
432
447
433
448
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;
435
451
436
452
function FindPythonDLL (APath : string): string;
437
453
Var
@@ -513,11 +529,15 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
513
529
PythonVersion.SysVersion := SysVersion;
514
530
PythonVersion.fSysArchitecture := PythonVersion.ExpectedArchitecture;
515
531
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 ;
521
541
end ;
522
542
523
543
{ $ENDIF}
0 commit comments