This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients Arfrever, docs@python, eryksun, martin.panter
Date 2016-08-29.10:07:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1472465256.32.0.4663975044.issue24045@psf.upfronthosting.co.za>
In-reply-to
Content
Unix waitpid() packs the process exit status and terminating signal number into a single status value. As specified by POSIX [1], the WEXITSTATUS function returns only the lower 8 bits of the process exit status. In theory, waitid() and wait6() can return the full 32-bit status value in the siginfo_t si_status. Apparently NetBSD does this [2], but evidently Linux does not:

    >>> p = subprocess.Popen(['python3', '-c','import os; os._exit(257)'])
    >>> os.waitid(os.P_PID, p.pid, os.WEXITED).si_status
    1

For Windows, programs sometimes return 16-bit WinAPI error codes or 32-bit HRESULT error codes. A critical error or unhandled exception may even result in returning a 32-bit NTSTATUS code (e.g. if you cancel the Windows error reporting dialog). 

Currently the Python 3 implementation of sys.exit and os._exit converts to a signed long, but the Windows exit code is an unsigned long. To use integer return codes in the range 0x80000000-0xFFFFFFFF requires manually converting to the corresponding negative signed value.

Technically the two error types that require 32-bit values are signed (i.e. HRESULT and NTSTATUS), so this shouldn't be a problem. However, in practice sometimes a function -- and definitely subprocess.Popen -- returns an HRESULT error code as an unsigned value. 

It would be convenient, at least on Windows, if handle_system_exit in Python/pythonrun.c converted the value using PyLong_AsUnsignedLongMask instead of PyLong_AsLong. Similarly os._exit could use Argument Clinic's "unsigned int" format, which also calls PyLong_AsUnsignedLongMask.

The problem with using PyLong_AsLong on Windows (32-bit or 64-bit) is that it overflows with an error value of -1 for values greater than 0x7FFFFFFF. In this case, os._exit raises OverflowError. handle_system_exit, on the other hand, ignores the exception. It just uses the -1 error value as the exit code, i.e. 0xFFFFFFFF. 

[1]: http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html
[2]: https://www.daemon-systems.org/man/wait.2.html
History
Date User Action Args
2016-08-29 10:07:36eryksunsetrecipients: + eryksun, Arfrever, docs@python, martin.panter
2016-08-29 10:07:36eryksunsetmessageid: <1472465256.32.0.4663975044.issue24045@psf.upfronthosting.co.za>
2016-08-29 10:07:36eryksunlinkissue24045 messages
2016-08-29 10:07:34eryksuncreate