Terminate Blender with Exit Code "1" when run from command line - python

I am working with an automated Blender python script and I would like to know how to terminate it with an Exit Code 1 when an Exception occurs.
The problem seems to be that the exit code from blender is always 0 even if the python script fails.
The following script definately produces a non-zero exit code, but blender sets exit code to 0
def main():
raise Exception("Fail")
sys.exit(1)
I also tried the --python-exit-code command line argument but to no effect:
C:\blender.exe --python-exit-code 2 --disable-abort-handler -P bake.py
this gives a slightly better result, because I get the following message:
Error: script failed, file: 'bake.py', exiting with code 2.
Unfortunately the exit code is still 0.
Can anyone enlighten me with some explanations or solutions on how I can exit the process with the correct exit code?
Thanks a lot for any hints!

--python-exit-code as stated in the documentation sets the exit code if the command line script called with --python raises an exception, not when it exits with a non-zero code.
Set the exit-code in [0..255] to exit if a Python exception is raised (only for scripts executed from the command line), zero disables.
Thus the only solution is to do your checking, and raise an exception manually, then catch it and exit in the except block. (If you don't exit right away, the exit code reverts to 0.
This code worked for me when doing unit tests inside a blender addon.
import unittest
from tests.my_test import MyTest
import sys
def run():
suite = unittest.defaultTestLoader.loadTestsFromTestCase(MyTest)
success = unittest.TextTestRunner().run(suite).wasSuccessful()
if not success:
raise Exception('Tests Failed')
try:
run()
except Exception:
sys.exit(1)

Related

Catching "Access denied" error when setting an environment variable using subprocess.call

I was attempting to set an environment variable and it failed due to access denied, however, the try-except block did not catch this as an error. I could catch the exit code myself and then check if the exit code is different than 0, but I was hoping a more pythonic way is possible.
This is what I have tried so perform:
try:
exit_code = subprocess.call(["SETX", "-m", "ENV_VAR", "Test"])
print 'exit_code: {}'.format(exit_code) # for debugging purposes
except Exception:
raise Exception("Could not set environment variable")
And this was the result:
ERROR: Access to the registry path is denied.
exit_code: 1
Process finished with exit code 0
As you can see, the exit code that is being returned from subprocess.call is 1, indicating a failure, however, the entire Python process is exiting with exit code 0 and "raise Exception" is never called.
What would be the best way to solve it?
Thanks to Alex K. for pointing out the correct usage. The fixed code:
try:
subprocess.check_call(["SETX", "-m", "ENV_VAR", "Test"])
except subprocess.CalledProcessError:
raise Exception("Could not set environment variable")

Exit shell script from python

I Have a shell script which in turn runs a python script, I have to exit from the main shell script when an exception is caught in python. Can anyone suggest a way on how to achieve it.
In Python you can set the return value using sys.exit(). Typically when execution completed successfully you return 0, and if not then some non-zero number.
So something like this in your Python will work:
import sys
try:
....
except:
sys.exit(1)
And then, as others have said, you need to make sure your bash script catches the error by either checking the return value explicitly (using e.g. $?) or using set -e.

python subprocess running unoconv throwing CalledProcessError exit code 8

I am trying to run unoconv to convert a set of documents in a folder like so:
import subprocess
try:
subprocess.check_call(['unoconv', "/home/foo/ve/pdf/pdf/pdf_media/" + <filename parameter>)
except subprocess.CalledProcessError as e:
print "conversion error: ", e
Now, I run the above code within a django view - and it runs as expects all the time i.e converts the document to PDF, but, sometimes, the above code throws me the following exception:
Command '['unoconv', u'/home/foo/ve/pdf/pdf/pdf_media/WgYozM7.doc']' returned non-zero exit status -8
However, when I go to the folder, I see that the conversion has happened and the PDF generated as expected.
I fail to understand what this exit code means (I searched the docs of unoconv but couldnt find any exit code 8).
It looks to me that your subprocess has a negative exit-code. This is not an application code, but caused by the OS terminating your program. The negative number is the signal-number the process received. On my mac, -8 stands for SIGFPE.

how to exit python execfile() with out exiting whole script

I have a code which runs mutile python codes such as below:
execfile("1.py")
execfile("2.py")
execfile("3.py")
however occasionally one of the above codes as an error, i put exit('error') in the code to cancell if there is an error. However i want the rest of the code to run and exit('error') exits the whole code, not just the execfile. How do i get the execfile to stop but the others to keep running?
The part of 1.py with exit() is:
try :
Ntot=10000
x,y,s=myMCMC2D(Ntot,0.78,0.63,1,1)
except :
exit('error')
try:
execfile('1.py')
except SystemExit:
print "1.py exited"
Exit is an exception which can be caught.

How can I have a python script safely exit itself?

Heres the scenario I have a password that must be entered if entered wrong the script will not proceed and just exit itself? But how can I tell the script to safely exit itself?
I tried sys.exit() but that gives a traceback error and doesn't seem like a very clean exit method.
In fact, sys.exit() will only throw a SystemExit exception, which would make the program exit if this exception wasn't catched, but I don't think it's poor coding.
Anyway, another possibility is to use os._exit(0) if you need to exit immediately at any cost. (cf. http://docs.python.org/2/library/exceptions.html#exceptions.SystemExit)
you are using the sys module, but you haven't imported it.
add this before the sys.exit() line :
import sys

Categories