@@ -1339,7 +1339,7 @@ TPythonInterface=class(TDynamicDll)
13391339 PyErr_BadInternalCall: procedure; cdecl;
13401340 PyErr_CheckSignals: function: integer; cdecl;
13411341 PyErr_Clear: procedure; cdecl;
1342- PyErr_Fetch: procedure( errtype, errvalue, errtraceback: PPPyObject ); cdecl;
1342+ PyErr_Fetch: procedure(out errtype, errvalue, errtraceback: PPyObject ); cdecl;
13431343 PyErr_NoMemory: function: PPyObject; cdecl;
13441344 PyErr_Occurred: function: PPyObject; cdecl;
13451345 PyErr_Print: procedure; cdecl;
@@ -1703,7 +1703,8 @@ TPythonTraceback = class
17031703 destructor Destroy; override;
17041704
17051705 procedure Clear ;
1706- procedure Refresh ;
1706+ procedure Refresh (pytraceback: PPyObject = nil );
1707+ procedure AddItem (const Context, FileName: string; LineNo: Integer);
17071708
17081709 property ItemCount : Integer read GetItemCount;
17091710 property Items[ idx : Integer ] : TTracebackItem read GetItem;
@@ -3771,6 +3772,18 @@ destructor TPythonTraceback.Destroy;
37713772 inherited ;
37723773end ;
37733774
3775+ procedure TPythonTraceback.AddItem (const Context, FileName: string;
3776+ LineNo: Integer);
3777+ var
3778+ Item: TTracebackItem;
3779+ begin
3780+ Item := TTracebackItem.Create;
3781+ Item.Context := Context;
3782+ Item.FileName := FileName;
3783+ Item.LineNo := LineNo;
3784+ FItems.Add(Item);
3785+ end ;
3786+
37743787procedure TPythonTraceback.Clear ;
37753788var
37763789 i : Integer;
@@ -5653,14 +5666,32 @@ procedure TPythonEngine.ListToSet( List : PPyObject; data : Pointer; size : Inte
56535666end ;
56545667
56555668procedure TPythonEngine.CheckError (ACatchStopEx : Boolean = False);
5669+ procedure ProcessSystemExit ;
5670+ var
5671+ errtype, errvalue, errtraceback: PPyObject;
5672+ SErrValue: string;
5673+ begin
5674+ PyErr_Fetch(errtype, errvalue, errtraceback);
5675+ Traceback.Refresh(errtraceback);
5676+ SErrValue := PyObjectAsString(errvalue);
5677+ PyErr_Clear;
5678+ raise EPySystemExit.CreateResFmt(@SPyExcSystemError, [SErrValue]);
5679+ end ;
5680+
5681+ var
5682+ PyException: PPyObject;
56565683begin
5657- if PyErr_Occurred <> nil then
5684+ PyException := PyErr_Occurred;
5685+ if PyException <> nil then
56585686 begin
5659- if ACatchStopEx and (PyErr_GivenExceptionMatches(PyErr_Occurred , PyExc_StopIteration^) <> 0 ) then
5687+ if ACatchStopEx and (PyErr_GivenExceptionMatches(PyException , PyExc_StopIteration^) <> 0 ) then
56605688 begin
56615689 PyErr_Clear;
5662- raise EPyStopIteration.Create( ' Stop iteration ' );
5690+ raise EPyStopIteration.CreateRes(@SPyExcStopIteration );
56635691 end
5692+ else if PyErr_GivenExceptionMatches(PyException, PyExc_SystemExit^) <> 0 then
5693+ // Special treatment for SystemExit. Calling PyErr_Print would terminate the process
5694+ ProcessSystemExit
56645695 else
56655696 begin
56665697 PyErr_Print;
0 commit comments