Tim Holzherr
Tim Holzherr

Reputation: 23

Python returns "wrong" exit code

My goal is to execute a python script (that executs some unittests) via a small *.bat script. The *.bat script should also receive the exit status of the python script. But it seems that the exit status of the *.py file is not transmitted correctly.

My testit.py:

execute_some_unittests()
   ...
   if success:
       sys.exit(1) 
   else:
       sys.exit(2)
   # End of the script

My test.bat:

python C:\testit.py
echo %ERRORLEVEL%

Depanding on the unittests I run in execute_some_unittests(), running test.bat results in 1, 2 or 0!!! I'm absolutly sure that the code reaches the sys.exit(1) (because i log everything) but nevertheless (depending on what unittests get tested) it goes wrong.

My guess is that executing the unittests is setting some flags which will be merged with the exit code and produce a unix like 16 bit exit code (instead of 8) and that the cmd is somwhat truncating my exit code...

Remarks:

I'm working since 2 days at this problem and will be gratefull for every idea...

I'm using Win7, python 2.6, modules: unittest, doctest, sys, os

Update: was asked for a full working example:

test.py:

import unittest
from scipy.interpolate import interp1d
class Test(unittest.TestCase):
    def test_basic(self):
        self.failUnlessEqual(1, 1)
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(Test)
    unittest.TextTestRunner(verbosity=2).run(suite)
    import sys
    sys.exit(42)

test.bat:

python C:\test.py
echo %ERRORLEVEL%

Result: error level 0 (whithout import scipy.interpolate it would be 42, but scipy is not the only module producing this problem)

Upvotes: 2

Views: 1565

Answers (1)

gecco
gecco

Reputation: 18840

This is a known error in the scipy module. See Issue "import scipy.sparse.sparsetools causes consistent exit code 0".

This issue seems still being open...

Upvotes: 1

Related Questions