2

I'm writing a python application that runs in a cross platform environment. Every once in awhile I have to use a sys.exit to stop the program when something goes wrong (or if something goes right). I'd like to use standardized exit codes beyond just 1 if something went wrong. Looking at the python os module documentation page (https://docs.python.org/3/library/os.html#os.EX_OK) I see a nice list of 16 posix standard exit codes.

I wanted to find if there were any win32 exit codes that correspond roughly to these posix codes. What I found was 16000 different exit codes: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

Fortunately, the first error message was ERROR_SUCCESS = 0x0, which corresponds nicely to os.EX_OK. I then looked for something that migh correspond to os.EX_SOFTWARE, but the only thing I found that might work is ERROR_FATAL_APP_EXIT = 0x2C9, but I'm not sure if there might be anything better. The list is too large to efficiently search through.

Is there any standard list (complete or incomplete) of WIN32 error codes that roughly correspond to posix codes? Or is there some agreed upon way to return cross-platform error codes?

Erotemic
  • 4,806
  • 4
  • 39
  • 80
  • 1
    The return value's semantics are give by the application author. If you want to use the POSIX exit codes, go ahead and use them. As long as your Python applications are documented to return POSIX exit codes, any code on any platform can check for them (if that is really necessary or helpful). But maybe I simply didn't understand the question. – IInspectable Oct 17 '16 at 19:37
  • Also see [Why is my program terminating with exit code 3?](https://blogs.msdn.microsoft.com/oldnewthing/20110519-00/?p=10623). TL;DR: *"There is no standard for process exit codes."* – IInspectable Oct 17 '16 at 19:39
  • Those "win32 exit codes" aren't exit codes, they're error codes. They correspond to POSIX errno values. As far I know there are no standardized Windows exit values, and the POSIX exit values are seldom used in practice. You might as well just use the POSIX values as is – Ross Ridge Oct 17 '16 at 19:39
  • Those "POSIX exit codes" defined by Python are not POSIX, and in fact [were just rejected from the Single Unix Specification a few months ago](http://austingroupbugs.net/view.php?id=957). They are really conventions (read: recommendations) provided by some BSDs (in their `` header file) and later adopted by Linux as well, and are not mandated by anything. – andlabs Oct 17 '16 at 19:51
  • The only thing you are required to do when writing a program is that exiting with 0 means success and any nonzero value means failure. (The C standard uses `EXIT_SUCCESS` but I'm not sure if anything will ever have that nonzero.) Even Windows and DOS obey this convention (`IF ERRORLEVEL`). – andlabs Oct 17 '16 at 19:58
  • @andlabs: that's not really mandatory either. Some programs (most notably robocopy) return non-zero values on success. – Harry Johnston Oct 17 '16 at 23:32
  • Exit codes with the high bit set (treated as negative by the command interpreter) should probably be avoided, because you may get such an exit code when an operating system exception occurs. Other than that, do as you please. – Harry Johnston Oct 17 '16 at 23:36

0 Answers0