Skip to content

Commit dffde0d

Browse files
committed
sync P4D, about TPythonThread
1 parent e1386d6 commit dffde0d

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

python4lazarus/PythonEngine.pas

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,27 +2607,23 @@ TPyVar = class(TPyObject)
26072607
{$HINTS OFF}
26082608
TPythonThread = class(TThread)
26092609
private
2610-
f_savethreadstate: PPyThreadState;
2611-
fInterpreterState: PPyInterpreterState;
26122610
fThreadState: PPyThreadState;
26132611
fThreadExecMode: TThreadExecMode;
2612+
private class threadvar
2613+
f_savethreadstate: PPyThreadState;
26142614

2615-
// Do not overwrite Execute! Use ExecuteWithPython instead!
2615+
// Do not overwrite Execute! Use ExecuteWithPython instead!
26162616
procedure Execute; override;
26172617
protected
26182618
procedure ExecuteWithPython; virtual; abstract;
2619-
2619+
public
26202620
procedure Py_Begin_Allow_Threads;
26212621
procedure Py_End_Allow_Threads;
2622-
// The following procedures are redundant and only for
2623-
// compatibility to the C API documentation.
2622+
// The following procedures are redundant and only for
2623+
// compatibility to the C API documentation.
26242624
procedure Py_Begin_Block_Threads;
26252625
procedure Py_Begin_Unblock_Threads;
26262626

2627-
public
2628-
property InterpreterState: PPyInterpreterState read fInterpreterState
2629-
write fInterpreterState
2630-
default nil;
26312627
property ThreadState: PPyThreadState read fThreadState
26322628
write fThreadState;
26332629
property ThreadExecMode: TThreadExecMode read fThreadExecMode
@@ -8431,29 +8427,22 @@ procedure TPyVar.SetValueFromVariant( const value : Variant );
84318427

84328428
procedure TPythonThread.Execute;
84338429
var
8434-
withinterp: Boolean;
84358430
global_state : PPyThreadState;
84368431
gilstate : PyGILState_STATE;
84378432
begin
8438-
withinterp := Assigned( fInterpreterState);
84398433
with GetPythonEngine do
84408434
begin
8441-
if withinterp then
8435+
if fThreadExecMode = emNewState then
84428436
begin
8443-
fThreadExecMode := emNewState;
8444-
fThreadState := PyThreadState_New( fInterpreterState);
8445-
if Assigned(fThreadState) then
8446-
begin
8447-
PyEval_AcquireThread(fThreadState);
8437+
gilstate := PyGILState_Ensure();
8438+
try
8439+
fThreadState := GetThreadState;
84488440
ExecuteWithPython;
8449-
PyEval_ReleaseThread( fThreadState);
8450-
PyThreadState_Clear( fThreadState);
8451-
PyThreadState_Delete( fThreadState);
8452-
end else
8453-
raise EPythonError.Create( 'Could not create a new thread state');
8454-
end else {withinterp}
8441+
finally
8442+
PyGILState_Release(gilstate);
8443+
end;
8444+
end else {fThreadExecMode}
84558445
begin
8456-
fThreadExecMode := emNewInterpreter;
84578446
gilstate := PyGILState_Ensure();
84588447
global_state := PyThreadState_Get;
84598448
PyThreadState_Swap(nil);

0 commit comments

Comments
 (0)