Skip to content

Commit

Permalink
fixing PythonVersionFromDLLName
Browse files Browse the repository at this point in the history
  • Loading branch information
Alextp committed Jan 14, 2021
1 parent eab8c8d commit 7046cd8
Showing 1 changed file with 36 additions and 20 deletions.
56 changes: 36 additions & 20 deletions python4lazarus/PythonEngine.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2671,7 +2671,7 @@ procedure FreeSubtypeInst(ob:PPyObject); cdecl;
procedure Register;
function PyType_HasFeature(AType : PPyTypeObject; AFlag : Integer) : Boolean;
function SysVersionFromDLLName(const DLLFileName : string): string;
procedure PythonVersionFromDLLName(const LibName: string; out MajorVersion, MinorVersion: integer);
procedure PythonVersionFromDLLName(LibName: string; out MajorVersion, MinorVersion: integer);

{ Helper functions}
(*
Expand Down Expand Up @@ -8835,29 +8835,45 @@ function IsPythonVersionRegistered(PythonVersion : AnsiString;
end;
{$ENDIF}

procedure PythonVersionFromDLLName(const LibName: string; out MajorVersion, MinorVersion: integer);
procedure PythonVersionFromDLLName(LibName: string; out MajorVersion, MinorVersion: integer);
//Windows: 'c:\some\path\python310.dll'
//Linux: '/some/path/libpython3.10.so'
const
cPython = 'python';
var
NPos: integer;
S: String;
begin
//Win: "python310.dll"
//Linux: "libpython3.10.so"
S := LibName;
NPos := Pos('python', LowerCase(S));
if NPos>0 then
begin
Inc(NPos, Length('python'));
MajorVersion := StrToIntDef(S[NPos], 3);
Inc(NPos);
if LibName[NPos]='.' then
Inc(NPos);
S := Copy(S, NPos);
NPos := Pos('.', S);
if NPos > 1 then
MinorVersion := StrToIntDef(Copy(S, 1, NPos-1), 3);
ch: char;
begin
MajorVersion:= 0;
MinorVersion:= 0;
LibName:= LowerCase(ExtractFileName(LibName)); //strip path
NPos:= Pos(cPython, LibName);
if NPos=0 then exit;
Inc(NPos, Length(cPython));
if NPos>Length(LibName) then exit;
ch:= LibName[NPos];
case ch of
'2'..'5': //support major versions 2...5, default 3
MajorVersion:= StrToIntDef(ch, 3);
else
exit;
end;
Delete(LibName, 1, NPos);
if LibName='' then exit;
case LibName[1] of
'.': //Unix name with dot
Delete(LibName, 1, 1);
'0'..'9': //Windows name w/o dot
begin end;
else //unknown char after major version
exit;
end;
NPos:= Pos('.', LibName); //dot with extension is required
if NPos=0 then exit;
Delete(LibName, NPos, MaxInt);
//support minor versions 0...MaxInt
MinorVersion:= StrToIntDef(LibName, 0);
end;


end.

0 comments on commit 7046cd8

Please sign in to comment.