Skip to content

Commit 0ffc49e

Browse files
committed
sync 3 units with P4D
1 parent bca9fb3 commit 0ffc49e

File tree

3 files changed

+48
-26
lines changed

3 files changed

+48
-26
lines changed

python4lazarus/PythonAction.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function TPythonAction.PythonClear(pself, args: PPyObject): PPyObject;
124124
ClearMethods;
125125
with GetPythonEngine, RegisteredMethods do
126126
begin
127-
Result := PyInt_FromLong(Count);
127+
Result := PyLong_FromLong(Count);
128128
end;
129129
end;
130130

@@ -146,7 +146,7 @@ function TPythonAction.PythonRegister(pself, args: PPyObject): PPyObject;
146146
begin
147147
Py_XINCREF(func);
148148
Add(func);
149-
Result := PyInt_FromLong(Count);
149+
Result := PyLong_FromLong(Count);
150150
end;
151151
end;
152152

@@ -168,7 +168,7 @@ function TPythonAction.PythonUnregister(pself, args: PPyObject): PPyObject;
168168
begin
169169
Py_XDECREF(func);
170170
Remove(func);
171-
Result := PyInt_FromLong(Count);
171+
Result := PyLong_FromLong(Count);
172172
end;
173173
end;
174174

python4lazarus/PythonVersions.pas

Lines changed: 42 additions & 20 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
@@ -92,7 +94,9 @@ implementation
9294
function TPythonVersion.GetDLLName: string;
9395
begin
9496
{$IFDEF MSWINDOWS}
95-
Result := 'python' + SysVersion[1] + SysVersion[3] + '.dll';
97+
Result := SysVersion;
98+
Delete(Result, 2, 1);
99+
Result := 'python' + Result + '.dll';
96100
{$ELSE}
97101
Result := 'libpython' + SysVersion + '.so';
98102
{$ENDIF}
@@ -400,7 +404,8 @@ function GetRegisteredPythonVersion(SysVersion: string;
400404
PythonVersion.IsRegistered := Result;
401405
end;
402406

403-
function GetRegisteredPythonVersions : TPythonVersions;
407+
function GetRegisteredPythonVersions(const MinVersion: string = '0.0';
408+
const MaxVersion: string = '100.100'): TPythonVersions;
404409
Var
405410
Count: Integer;
406411
I: Integer;
@@ -409,27 +414,40 @@ function GetRegisteredPythonVersions : TPythonVersions;
409414
Count := 0;
410415
SetLength(Result, High(PYTHON_KNOWN_VERSIONS));
411416
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;
412422
if GetRegisteredPythonVersion(PYTHON_KNOWN_VERSIONS[I].RegVersion, PythonVersion) then
413423
begin
414424
Result[Count] := PythonVersion;
415425
Inc(Count);
416426
end;
427+
end;
417428
SetLength(Result, Count);
418429
end;
419430

420-
function GetLatestRegisteredPythonVersion(out PythonVersion: TPythonVersion): Boolean;
431+
function GetLatestRegisteredPythonVersion(out PythonVersion: TPythonVersion;
432+
const MinVersion: string = '0.0'; const MaxVersion: string = '100.100'): Boolean;
421433
Var
422434
I: Integer;
423435
begin
436+
Result := False;
424437
for I := High(PYTHON_KNOWN_VERSIONS) downto 1 do
425438
begin
426-
Result := GetRegisteredPythonVersion(PYTHON_KNOWN_VERSIONS[I].RegVersion, PythonVersion);
427-
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);
428445
end;
429446
end;
430447

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

434452
function FindPythonDLL(APath : string): string;
435453
Var
@@ -438,15 +456,15 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
438456
DLLFileName: string;
439457
begin
440458
Result := '';
441-
Handle := FindFirstFile(PWideChar(APath+'\python??.dll'), FindFileData);
459+
Handle := FindFirstFile(PWideChar(APath+'\python*.dll'), FindFileData);
442460
if Handle = INVALID_HANDLE_VALUE then Exit; // not python dll
443461
DLLFileName:= FindFileData.cFileName;
444462
// skip if python3.dll was found
445-
if Length(DLLFileName) <> 12 then FindNextFile(Handle, FindFileData);
463+
if Length(DLLFileName) <= 11 then FindNextFile(Handle, FindFileData);
446464
if Handle = INVALID_HANDLE_VALUE then Exit;
447465
Windows.FindClose(Handle);
448466
DLLFileName:= FindFileData.cFileName;
449-
if Length(DLLFileName) = 12 then
467+
if Length(DLLFileName) > 11 then
450468
Result := DLLFileName;
451469
end;
452470

@@ -506,16 +524,20 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
506524
end;
507525
PythonVersion.DLLPath := DLLPath;
508526

509-
SysVersion := GetPythonVersionFromDLLName(DLLFileName);
527+
SysVersion := SysVersionFromDLLName(DLLFileName);
510528

511529
PythonVersion.SysVersion := SysVersion;
512530
PythonVersion.fSysArchitecture := PythonVersion.ExpectedArchitecture;
513531

514-
for I := High(PYTHON_KNOWN_VERSIONS) downto 1 do
515-
if PYTHON_KNOWN_VERSIONS[I].RegVersion = SysVersion then begin
516-
Result := True;
517-
break;
518-
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;
519541
end;
520542

521543
{$ENDIF}

python4lazarus/VarPyth.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function MainModule: Variant; // return the main module that's used for executin
107107
function BuiltinModule: Variant; // return the builtin module
108108
function SysModule: Variant; // return the builtin module 'sys'
109109
function DatetimeModule: Variant; // return the builtin module 'datetime'
110-
function Import( const AModule : AnsiString ): Variant; // import a Python module and return the module object.
110+
function Import(const AModule: string): Variant; // import a Python module and return the module object.
111111
function len(const AValue : Variant ): NativeInt; // return the length of a Python collection.
112112
function _type(const AValue : Variant ): Variant; // return the type object of a Python object.
113113
function iter(const AValue : Variant ): Variant; // return an iterator for the container AValue. You can call the 'next' method of the iterator until you catch the EPyStopIteration exception.
@@ -691,7 +691,7 @@ function MainModule : Variant;
691691

692692
function BuiltinModule : Variant;
693693
begin
694-
Result := Import(AnsiString(GetPythonEngine.BuiltInModuleName));
694+
Result := Import(GetPythonEngine.BuiltInModuleName);
695695
end;
696696

697697
function SysModule : Variant;
@@ -704,7 +704,7 @@ function DatetimeModule : Variant; // return the builtin module 'datetime'
704704
Result := Import('datetime');
705705
end;
706706

707-
function Import( const AModule : AnsiString ) : Variant;
707+
function Import(const AModule: string): Variant;
708708
var
709709
_module : PPyObject;
710710
_module_name : PPyObject;

0 commit comments

Comments
 (0)