Skip to content

Commit 958d87d

Browse files
committed
Better fix for python/cpython#100171
1 parent a2f3b82 commit 958d87d

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

Source/PythonEngine.pas

+3-20
Original file line numberDiff line numberDiff line change
@@ -3033,7 +3033,9 @@ function TDynamicDll.GetDllPath : string;
30333033

30343034
if (DLLPath = '') and not FInExtensionModule then begin
30353035
{$IFDEF MSWINDOWS}
3036-
IsPythonVersionRegistered(RegVersion, Result, AllUserInstall);
3036+
if IsPythonVersionRegistered(RegVersion, Result, AllUserInstall) and (Self is TPythonEngine) then
3037+
// https://github.com/python/cpython/issues/100171
3038+
TPythonEngine(Self).SetPythonHome(Result);
30373039
{$ENDIF}
30383040
{$IFDEF DARWIN}
30393041
Result := '/Library/Frameworks/Python.framework/Versions/' + RegVersion + '/lib/';
@@ -4466,7 +4468,6 @@ procedure TPythonEngine.Initialize;
44664468

44674469
var
44684470
i : Integer;
4469-
WorkAround: AnsiString;
44704471
begin
44714472
if Assigned(gPythonEngine) then
44724473
raise Exception.Create('There is already one instance of TPythonEngine running' );
@@ -4505,24 +4506,6 @@ procedure TPythonEngine.Initialize;
45054506
if not Initialized then
45064507
Initialize;
45074508

4508-
// WorkAround for https://github.com/python/cpython/issues/100171
4509-
if (MajorVersion = 3) and (MinorVersion >= 11) then
4510-
begin
4511-
WorkAround :=
4512-
'import sys'#13#10 + //0
4513-
'if sys.version_info > (3,11,0):'#13#10 + //1
4514-
' import os'#13#10 + //2
4515-
''#13#10 + //3
4516-
' dllpath = os.path.join(sys.base_prefix, ''DLLs'')'#13#10 + //4
4517-
' if dllpath not in sys.path:'#13#10 + //5
4518-
' sys.path.insert(3, dllpath)'#13#10 + //6
4519-
''#13#10 + //7
4520-
' del dllpath'#13#10 + //8
4521-
' del os'#13#10 + //9
4522-
'del sys'#13#10; //10
4523-
ExecString(WorkAround);
4524-
end;
4525-
45264509
if InitScript.Count > 0 then
45274510
ExecStrings(InitScript);
45284511
if Assigned(FOnAfterInit) then

Source/PythonVersions.pas

+5-4
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,14 @@ procedure TPythonVersion.AssignTo(PythonEngine: TPersistent);
123123
if Is_venv then begin
124124
TPythonEngine(PythonEngine).VenvPythonExe := PythonExecutable;
125125
TPythonEngine(PythonEngine).SetPythonHome(DLLPath);
126-
end else if not IsRegistered or Is_conda then
126+
end else
127127
{
128-
Not sure why but PythonHome needs to be set even for
129-
registered conda distributions
130-
Note also that for conda distributions to work properly,
128+
Note that for conda distributions to work properly,
131129
you need to add Format('%s;%0:s\Library\bin;', [Version.InstallPath]
132130
to your Windows path if it is not there already.
131+
132+
Following the advice in https://github.com/python/cpython/issues/100171
133+
SetPythonHome is called even for registered versions
133134
}
134135
TPythonEngine(PythonEngine).SetPythonHome(InstallPath);
135136
end;

0 commit comments

Comments
 (0)