@@ -2607,27 +2607,23 @@ TPyVar = class(TPyObject)
2607
2607
{ $HINTS OFF}
2608
2608
TPythonThread = class (TThread)
2609
2609
private
2610
- f_savethreadstate: PPyThreadState;
2611
- fInterpreterState: PPyInterpreterState;
2612
2610
fThreadState: PPyThreadState;
2613
2611
fThreadExecMode: TThreadExecMode;
2612
+ private class threadvar
2613
+ f_savethreadstate: PPyThreadState;
2614
2614
2615
- // Do not overwrite Execute! Use ExecuteWithPython instead!
2615
+ // Do not overwrite Execute! Use ExecuteWithPython instead!
2616
2616
procedure Execute ; override;
2617
2617
protected
2618
2618
procedure ExecuteWithPython ; virtual ; abstract ;
2619
-
2619
+ public
2620
2620
procedure Py_Begin_Allow_Threads ;
2621
2621
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.
2624
2624
procedure Py_Begin_Block_Threads ;
2625
2625
procedure Py_Begin_Unblock_Threads ;
2626
2626
2627
- public
2628
- property InterpreterState: PPyInterpreterState read fInterpreterState
2629
- write fInterpreterState
2630
- default nil ;
2631
2627
property ThreadState: PPyThreadState read fThreadState
2632
2628
write fThreadState;
2633
2629
property ThreadExecMode: TThreadExecMode read fThreadExecMode
@@ -8431,29 +8427,22 @@ procedure TPyVar.SetValueFromVariant( const value : Variant );
8431
8427
8432
8428
procedure TPythonThread.Execute ;
8433
8429
var
8434
- withinterp: Boolean;
8435
8430
global_state : PPyThreadState;
8436
8431
gilstate : PyGILState_STATE;
8437
8432
begin
8438
- withinterp := Assigned( fInterpreterState);
8439
8433
with GetPythonEngine do
8440
8434
begin
8441
- if withinterp then
8435
+ if fThreadExecMode = emNewState then
8442
8436
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;
8448
8440
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}
8455
8445
begin
8456
- fThreadExecMode := emNewInterpreter;
8457
8446
gilstate := PyGILState_Ensure();
8458
8447
global_state := PyThreadState_Get;
8459
8448
PyThreadState_Swap(nil );
0 commit comments