Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bddf7c8

Browse files
committed
Fix Initialization for python versions 3.8 and 3.9.
Added error handling for initialization errors.
1 parent b2512c6 commit bddf7c8

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

Source/PythonEngine.pas

+26-7
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,8 @@ PyConfig = record
10101010
ConfigOffests: TConfigOffsets =
10111011
{$IFDEF MSWINDOWS}
10121012
{$IFDEF CPU64BITS}
1013-
((8, 80, 88, 144, 156, 160, 164, 172, 216, 104, 232, 240, 248, 264),
1014-
(8, 80, 88, 144, 156, 160, 164, 172, 216, 104, 232, 240, 248, 264),
1013+
((8, 80, 88, 144, 156, 160, 164, 172, 224, 104, 240, 248, 256, 272),
1014+
(8, 80, 88, 144, 156, 160, 164, 172, 224, 104, 240, 248, 256, 272),
10151015
(8, 80, 104, 152, 168, 172, 176, 184, 232, 240, 256, 272, 280, 296),
10161016
(8, 96, 120, 168, 184, 188, 192, 200, 264, 272, 288, 304, 312, 336),
10171017
(8, 96, 120, 168, 184, 188, 192, 200, 268, 272, 288, 304, 312, 336),
@@ -3036,6 +3036,8 @@ implementation
30363036
SPyConvertionError = 'Conversion Error: %s expects a %s Python object';
30373037
SPyExcStopIteration = 'Stop Iteration';
30383038
SPyExcSystemError = 'Unhandled SystemExit exception. Code: %s';
3039+
SPyInitFailed = 'Python initialization failed: %s';
3040+
SPyInitFailedUnknown = 'Unknown initialization error';
30393041

30403042
(*******************************************************)
30413043
(** **)
@@ -4720,6 +4722,8 @@ procedure TPythonEngine.Initialize;
47204722
var
47214723
i : Integer;
47224724
Config: PyConfig;
4725+
Status: PyStatus;
4726+
ErrMsg: string;
47234727
begin
47244728
if Assigned(gPythonEngine) then
47254729
raise Exception.Create('There is already one instance of TPythonEngine running' );
@@ -4764,15 +4768,30 @@ procedure TPythonEngine.Initialize;
47644768
if Assigned(FOnConfigInit) then
47654769
FOnConfigInit(Self, Config);
47664770

4767-
Py_InitializeFromConfig(Config);
4771+
Status := Py_InitializeFromConfig(Config);
4772+
FInitialized := Py_IsInitialized() <> 0
47684773
finally
47694774
PyConfig_Clear(Config);
47704775
end;
47714776

4772-
if Assigned(Py_IsInitialized) then
4773-
FInitialized := Py_IsInitialized() <> 0
4774-
else
4775-
FInitialized := True;
4777+
if not FInitialized then
4778+
begin
4779+
if PyStatus_Exception(Status) then
4780+
ErrMsg := Format(SPyInitFailed, [string(Status.err_msg)])
4781+
else
4782+
ErrMsg := Format(SPyInitFailed, [SPyInitFailedUnknown]);
4783+
if FatalMsgDlg then
4784+
{$IFDEF MSWINDOWS}
4785+
MessageBox( GetActiveWindow, PChar(ErrMsg), 'Error', MB_TASKMODAL or MB_ICONSTOP );
4786+
{$ELSE}
4787+
WriteLn(ErrOutput, ErrMsg);
4788+
{$ENDIF}
4789+
if FatalAbort then
4790+
Quit
4791+
else
4792+
raise Exception.Create(ErrMsg);
4793+
end;
4794+
47764795
InitSysPath;
47774796
if RedirectIO and Assigned(FIO) then
47784797
DoRedirectIO;

0 commit comments

Comments
 (0)