@@ -9006,29 +9006,45 @@ function IsPythonVersionRegistered(PythonVersion : string;
90069006end ;
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' ;
90109014var
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 );
90309047end ;
90319048
9032-
90339049end .
90349050
0 commit comments