Skip to content

Commit 670fe32

Browse files
committed
Check the executable instead of the dll for X64.
Check for venv
1 parent 126632f commit 670fe32

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

PythonForDelphi/Components/Sources/Core/PythonVersions.pas

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ TPythonVersion = record
3737
DLLPath: string;
3838
InstallPath: string;
3939
PythonPath: string;
40+
function Is_venv: Boolean;
4041
procedure AssignTo(PythonEngine: TPersistent);
4142
property PythonExecutable: string read GetPythonExecutable;
4243
property DLLName: string read GetDLLName;
@@ -58,6 +59,8 @@ TPythonVersion = record
5859

5960

6061
{$IFDEF MSWINDOWS}
62+
(* Checks whether an executable was compiled for X64 *)
63+
function IsEXEx64(const EXEName: string): Boolean;
6164
(* Checks whether a DLL was compiled for X64 *)
6265
function Isx64(const FileName: string): Boolean;
6366
(* Checks whether a Python version is registered and returns the related info *)
@@ -110,7 +113,8 @@ function TPythonVersion.ExpectedArchitecture: string;
110113

111114
procedure TPythonVersion.AssignTo(PythonEngine: TPersistent);
112115
begin
113-
if PythonEngine is TPythonEngine then begin
116+
if PythonEngine is TPythonEngine then
117+
begin
114118
TPythonEngine(PythonEngine).UseLastKnownVersion := False;
115119
TPythonEngine(PythonEngine).RegVersion := SysVersion;
116120
TPythonEngine(PythonEngine).DllName := DLLName;
@@ -177,6 +181,16 @@ function TPythonVersion.GetSysArchitecture: string;
177181
Result := 'Unknown';
178182
end;
179183

184+
function TPythonVersion.Is_venv: Boolean;
185+
{
186+
Check weather this is python venv folder introduced in python 3.5
187+
Note: venv is different from virtualenv
188+
}
189+
begin
190+
Result := not IsRegistered and (InstallPath <> DLLPath) and
191+
FileExists(IncludeTrailingPathDelimiter(InstallPath) + 'pyvenv.cfg');
192+
end;
193+
180194
function CompareVersions(A, B : String) : Integer;
181195

182196
function GetNextNumber(var Version: string): Integer;
@@ -220,6 +234,15 @@ function CompareVersions(A, B : String) : Integer;
220234
end;
221235

222236
{$IFDEF MSWINDOWS}
237+
function IsEXEx64(const EXEName: string): Boolean;
238+
var
239+
BinaryType: DWORD;
240+
begin
241+
Result := FileExists(EXEName) and
242+
GetBinaryType(PChar(ExeName), Binarytype) and
243+
(BinaryType = SCS_64BIT_BINARY);
244+
end;
245+
223246
function Isx64(const FileName: string): Boolean;
224247
var
225248
Strm : TFileStream;
@@ -374,7 +397,7 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
374397
DLLFileName: string;
375398
begin
376399
Result := '';
377-
Handle := FindFirstFile(PWideChar(Path+'\python??.dll'), FindFileData);
400+
Handle := FindFirstFile(PWideChar(APath+'\python??.dll'), FindFileData);
378401
if Handle = INVALID_HANDLE_VALUE then Exit; // not python dll
379402
DLLFileName:= FindFileData.cFileName;
380403
// skip if python3.dll was found
@@ -408,7 +431,7 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
408431

409432
// check if same platform
410433
try
411-
if {$IFDEF CPUX64}not {$ENDIF}Isx64(DLLPath+'\'+DLLFileName) then Exit;
434+
if {$IFDEF CPUX64}not {$ENDIF}IsEXEx64(DLLPath+'\python.exe') then Exit;
412435
except
413436
Exit;
414437
end;

0 commit comments

Comments
 (0)