Jump to content
Sign in to follow this  
gmbouwer

Getting exitcode (%errorlevel%) of python script

Recommended Posts

Hi

 

Apologies if this has an obvious answer but it seems I don't know what to search for. Please refer me to previous posts or any existing documentation.

 

I need to get the exitcode (%errorlevel%) value of a python script. 

function ExecPython(lines: TStrings): integer
begin
try
  PythonEngine1.ExecStrings(lines);
  result := 0;  // success
except
  on E: Exception
    result := GetLastError()
end
end;

 

I'm using older version of Delphi.

 

Regards

gmb

Share this post


Link to post

PythonEngine1.ExecStrings(lines)

 

will raise in exception (a subclass of EPyException) if an error occurs.   If you redirecting the python output using for instance TPythonGUIInputOutput then error information is printed and you will be able to see where the error occurred including a traceback.   You can also use the Traceback object to extract information about the error.  If you want to handle the Exception you can use

 

try
  PythonEngine1.ExecStrings(lines);
except
  on E: EPyException
    // Do whatever you want. You can use the PythonEngine1.Traceback to get information about what went wrong
end

Share this post


Link to post

Thanks pyscripter for the prompt response.

 

My use-case: the python code to be executed can return any one of a number of pre-defined result codes which in turn will determine the next action to take.

E.g.

function ExecPython(lines: TStrings): integer 
....
begin
  script_result := ExecPython(python_script);
  case script_result of
    0: Showmessage('All good');
    101: Showmessage('Retry using some other setup');
    102: Showmessage('Connection failed, please re-connect');
    // etc
  end;
end;

I was hoping for something as simple as my code sample where the integer return value can be checked; but I guess if we need to parse the output (Traceback, Exception.Message, etc) it could work.

I have access to the python code library; it will be possible to change the exception messages to something more "easily parse-able". 

To be honest I was expecting something as simple as 

PythonEngine1.LastResult;

 

I'll investigate the Traceback suggestion further.

 

Regards

Share this post


Link to post

You can do something similar in python.   You can use sys.exit(n) which raises the SystemExit exception.   n can be a number or a string or anything else.    But if you call sys.exit(n)  with different integer values then you can do the following:

try
  PythonEngine.ExecString(...);
except
   on E: EPySystemExit do
     begin
        case IntToStr(e.EValue):
           1:
           2: 
         end;
     end;
end;

 

Edited by pyscripter

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×