@@ -2670,6 +2670,8 @@ procedure PyObjectDestructor( pSelf : PPyObject); cdecl;
26702670procedure FreeSubtypeInst (ob:PPyObject); cdecl;
26712671procedure Register ;
26722672function PyType_HasFeature (AType : PPyTypeObject; AFlag : Integer) : Boolean;
2673+ function SysVersionFromDLLName (const DLLFileName : string): string;
2674+ procedure PythonVersionFromDLLName (const LibName: string; out MajorVersion, MinorVersion: integer);
26732675
26742676{ Helper functions}
26752677(*
@@ -2698,8 +2700,6 @@ procedure MaskFPUExceptions(ExceptionsMasked : boolean;
26982700*)
26992701function CleanString (const s : AnsiString; AppendLF : Boolean = True) : AnsiString; overload;
27002702
2701- procedure DetectPythonVersionFromLibName (const LibName: string; out MajorVersion, MinorVersion: integer);
2702-
27032703implementation
27042704
27052705{ $R *.dcr} // AT
@@ -2723,29 +2723,6 @@ function _IsBufferAscii(buf: PAnsiChar): boolean; inline;
27232723 Result := true;
27242724end ;
27252725
2726- procedure DetectPythonVersionFromLibName (const LibName: string; out MajorVersion, MinorVersion: integer);
2727- var
2728- NPos: integer;
2729- S: String;
2730- begin
2731- // Win: "python310.dll"
2732- // Linux: "libpython3.10.so"
2733- S := LibName;
2734- NPos := Pos(' python' , LowerCase(S));
2735- if NPos>0 then
2736- begin
2737- Inc(NPos, Length(' python' ));
2738- MajorVersion := StrToIntDef(S[NPos], 3 );
2739- Inc(NPos);
2740- if LibName[NPos]=' .' then
2741- Inc(NPos);
2742- S := Copy(S, NPos);
2743- NPos := Pos(' .' , S);
2744- if NPos > 1 then
2745- MinorVersion := StrToIntDef(Copy(S, 1 , NPos-1 ), 3 );
2746- end ;
2747- end ;
2748-
27492726(* ******************************************************)
27502727(* * **)
27512728(* * Globals **)
@@ -3124,8 +3101,8 @@ constructor TPythonInterface.Create(AOwner: TComponent);
31243101procedure TPythonInterface.AfterLoad ;
31253102begin
31263103 inherited ;
3104+ PythonVersionFromDLLName(DLLName, FMajorVersion, FMinorVersion);
31273105
3128- DetectPythonVersionFromLibName(DLLName, FMajorVersion, FMinorVersion);
31293106 FBuiltInModuleName := ' builtins' ;
31303107
31313108 try
@@ -4055,10 +4032,7 @@ procedure TPythonEngine.DoOpenDll(const aDllName : String);
40554032 end ;
40564033 end
40574034 else
4058- begin
4059- DetectPythonVersionFromLibName(aDllName, NMajor, NMinor);
4060- RegVersion := Format(' %d.%d' , [NMajor, NMinor]);
4061- end ;
4035+ RegVersion := SysVersionFromDLLName(aDllName);
40624036 inherited ;
40634037end ;
40644038
@@ -8774,6 +8748,14 @@ procedure Register;
87748748 TPythonType, TPythonModule, TPythonDelphiVar]);
87758749end ;
87768750
8751+ function SysVersionFromDLLName (const DLLFileName : string): string;
8752+ var
8753+ Minor, Major: integer;
8754+ begin
8755+ PythonVersionFromDLLName(DLLFileName, Major, Minor);
8756+ Result := Format(' %d.%d' , [Major, Minor]);
8757+ end ;
8758+
87778759function PyType_HasFeature (AType : PPyTypeObject; AFlag : Integer) : Boolean;
87788760begin
87798761 // (((t)->tp_flags & (f)) != 0)
@@ -8853,5 +8835,29 @@ function IsPythonVersionRegistered(PythonVersion : AnsiString;
88538835end ;
88548836{ $ENDIF}
88558837
8838+ procedure PythonVersionFromDLLName (const LibName: string; out MajorVersion, MinorVersion: integer);
8839+ var
8840+ NPos: integer;
8841+ S: String;
8842+ begin
8843+ // Win: "python310.dll"
8844+ // Linux: "libpython3.10.so"
8845+ S := LibName;
8846+ NPos := Pos(' python' , LowerCase(S));
8847+ if NPos>0 then
8848+ begin
8849+ Inc(NPos, Length(' python' ));
8850+ MajorVersion := StrToIntDef(S[NPos], 3 );
8851+ Inc(NPos);
8852+ if LibName[NPos]=' .' then
8853+ Inc(NPos);
8854+ S := Copy(S, NPos);
8855+ NPos := Pos(' .' , S);
8856+ if NPos > 1 then
8857+ MinorVersion := StrToIntDef(Copy(S, 1 , NPos-1 ), 3 );
8858+ end ;
8859+ end ;
8860+
8861+
88568862end .
88578863
0 commit comments