Skip to content

Commit 6fe44aa

Browse files
committed
LoadDLLInExtensionModule added
1 parent e4e481a commit 6fe44aa

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

Modules/DemoModule/uMain.pas

+2-4
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,14 @@ function PyInit_DemoModule: PPyObject;
5050
gEngine := TPythonEngine.Create(nil);
5151
gEngine.AutoFinalize := False;
5252
gEngine.UseLastKnownVersion := False;
53-
// Adapt to the desired python version
54-
gEngine.RegVersion := '3.8';
55-
gEngine.DllName := 'python38.dll';
53+
gEngine.UseLastKnownVersion := True;
5654

5755
gModule := TPythonModule.Create(nil);
5856
gModule.Engine := gEngine;
5957
gModule.ModuleName := 'DemoModule';
6058
gModule.AddMethod('is_prime', delphi_is_prime, 'is_prime(n) -> bool' );
6159

62-
gEngine.LoadDll;
60+
gEngine.LoadDllInExtensionModule;
6361
except
6462
end;
6563
Result := gModule.Module;

Source/PythonEngine.pas

+34-18
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,7 @@ TDynamicDll = class(TComponent)
11701170
FOnBeforeLoad : TNotifyEvent;
11711171
FOnAfterLoad : TNotifyEvent;
11721172
FOnBeforeUnload : TNotifyEvent;
1173+
FInExtensionModule : Boolean;
11731174

11741175
function Import(const funcname: AnsiString; canFail : Boolean = True): Pointer;
11751176
procedure Loaded; override;
@@ -1189,6 +1190,7 @@ TDynamicDll = class(TComponent)
11891190
procedure OpenDll(const aDllName : string);
11901191
function IsHandleValid : Boolean;
11911192
procedure LoadDll;
1193+
procedure LoadDllInExtensionModule;
11921194
procedure UnloadDll;
11931195
procedure Quit;
11941196

@@ -2978,7 +2980,7 @@ function TDynamicDll.GetDllPath : string;
29782980
begin
29792981
Result := DllPath;
29802982

2981-
if DLLPath = '' then begin
2983+
if (DLLPath = '') and not FInExtensionModule then begin
29822984
{$IFDEF MSWINDOWS}
29832985
IsPythonVersionRegistered(RegVersion, Result, AllUserInstall);
29842986
{$ENDIF}
@@ -3086,6 +3088,12 @@ procedure TDynamicDll.LoadDll;
30863088
OpenDll( DllName );
30873089
end;
30883090

3091+
procedure TDynamicDll.LoadDllInExtensionModule;
3092+
begin
3093+
FInExtensionModule := True;
3094+
LoadDLL;
3095+
end;
3096+
30893097
procedure TDynamicDll.UnloadDll;
30903098
begin
30913099
if IsHandleValid then begin
@@ -4223,26 +4231,34 @@ procedure TPythonEngine.Initialize;
42234231
raise Exception.Create('There is already one instance of TPythonEngine running' );
42244232

42254233
gPythonEngine := Self;
4226-
CheckRegistry;
4227-
if Assigned(Py_SetProgramName) and (Length(FProgramName) > 0) then
4228-
Py_SetProgramName(PWCharT(FProgramName));
4229-
AssignPyFlags;
4230-
if Length(FPythonHome) > 0 then
4231-
Py_SetPythonHome(PWCharT(FPythonHome));
4232-
Py_Initialize;
4233-
if Assigned(Py_IsInitialized) then
4234-
FInitialized := Py_IsInitialized() <> 0
4235-
else
4236-
FInitialized := True;
4234+
42374235
FIORedirected := False;
4238-
InitSysPath;
4239-
SetProgramArgs;
4236+
if FInExtensionModule then
4237+
FInitialized := True
4238+
else
4239+
begin
4240+
CheckRegistry;
4241+
if Assigned(Py_SetProgramName) and (Length(FProgramName) > 0) then
4242+
Py_SetProgramName(PWCharT(FProgramName));
4243+
AssignPyFlags;
4244+
if Length(FPythonHome) > 0 then
4245+
Py_SetPythonHome(PWCharT(FPythonHome));
4246+
Py_Initialize;
4247+
if Assigned(Py_IsInitialized) then
4248+
FInitialized := Py_IsInitialized() <> 0
4249+
else
4250+
FInitialized := True;
4251+
InitSysPath;
4252+
SetProgramArgs;
4253+
if InitThreads and Assigned(PyEval_InitThreads) then
4254+
PyEval_InitThreads;
4255+
if RedirectIO and Assigned(FIO) then
4256+
DoRedirectIO;
4257+
end;
4258+
42404259
GetTimeStructType;
42414260
GetDateTimeTypes;
4242-
if InitThreads and Assigned(PyEval_InitThreads) then
4243-
PyEval_InitThreads;
4244-
if RedirectIO and Assigned(FIO) then
4245-
DoRedirectIO;
4261+
42464262
for i := 0 to ClientCount - 1 do
42474263
with Clients[i] do
42484264
if not Initialized then

0 commit comments

Comments
 (0)