Skip to content

Commit e6080ff

Browse files
committed
1 parent 9ff1d7c commit e6080ff

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

Source/PythonEngine.pas

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9006,29 +9006,45 @@ function IsPythonVersionRegistered(PythonVersion : string;
90069006
end;
90079007
{$ENDIF}
90089008

9009-
procedure PythonVersionFromDLLName(const LibName: string; out MajorVersion, MinorVersion: integer);
9009+
procedure PythonVersionFromDLLName(LibName: string; out MajorVersion, MinorVersion: integer);
9010+
//Windows: 'c:\some\path\python310.dll'
9011+
//Linux: '/some/path/libpython3.10.so'
9012+
const
9013+
cPython = 'python';
90109014
var
90119015
NPos: integer;
9012-
S: String;
9013-
begin
9014-
//Win: "python310.dll"
9015-
//Linux: "libpython3.10.so"
9016-
S := LibName;
9017-
NPos := Pos('python', LowerCase(S));
9018-
if NPos>0 then
9019-
begin
9020-
Inc(NPos, Length('python'));
9021-
MajorVersion := StrToIntDef(S[NPos], 3);
9022-
Inc(NPos);
9023-
if LibName[NPos]='.' then
9024-
Inc(NPos);
9025-
S := Copy(S, NPos);
9026-
NPos := Pos('.', S);
9027-
if NPos > 1 then
9028-
MinorVersion := StrToIntDef(Copy(S, 1, NPos-1), 3);
9029-
end;
9016+
ch: char;
9017+
begin
9018+
MajorVersion:= 0;
9019+
MinorVersion:= 0;
9020+
LibName:= LowerCase(ExtractFileName(LibName)); //strip path
9021+
NPos:= Pos(cPython, LibName);
9022+
if NPos=0 then exit;
9023+
Inc(NPos, Length(cPython));
9024+
if NPos>Length(LibName) then exit;
9025+
ch:= LibName[NPos];
9026+
case ch of
9027+
'2'..'5': //support major versions 2...5, default 3
9028+
MajorVersion:= StrToIntDef(ch, 3);
9029+
else
9030+
exit;
9031+
end;
9032+
Delete(LibName, 1, NPos);
9033+
if LibName='' then exit;
9034+
case LibName[1] of
9035+
'.': //Unix name with dot
9036+
Delete(LibName, 1, 1);
9037+
'0'..'9': //Windows name w/o dot
9038+
begin end;
9039+
else //unknown char after major version
9040+
exit;
9041+
end;
9042+
NPos:= Pos('.', LibName); //dot with extension is required
9043+
if NPos=0 then exit;
9044+
Delete(LibName, NPos, MaxInt);
9045+
//support minor versions 0...MaxInt
9046+
MinorVersion:= StrToIntDef(LibName, 0);
90309047
end;
90319048

9032-
90339049
end.
90349050

0 commit comments

Comments
 (0)