ISO/ANSI C strerror indicates out of range error by setting errno, but existing code incorrectly checks for NULL return value. Attached patch (tested n Mac OS X) makes os.strerror raise ValueError for out of range argument.
my manpage (debian testing) states: POSIX.1-2001 permits strerror() to set errno if the call encounters an error, but does not specify what value should be returned as the func‐ tion result in the event of an error. On some systems, strerror() returns NULL if the error number is unknown. On other systems, str‐ error() returns a string something like "Error nnn occurred" and sets errno to EINVAL if the error number is unknown. So, I think this patch should be rejected.
And on some system "Unknown error: nnn" is returned with no error indication. Your concern that the patch is invalid on some unidentified system. This concern can easily be addressed by checking for NULL return *in addition* to the errno check. The real question is whether it is desirable to raise ValueError from strerror() when error code is out of bound. I would say the existing code intends to do exactly that, but th error check is incorrect on at least one popular platform. I believe it is better to raise an error because as a user, seeing "unknown error has occurred" message, is one of the worst experiences. On the other hand, if the consensus is that strerror() should always (short of out of memory condition) return a string, then (assuming null return is a possibility) the code needs to be changed to return "Unknown error: nnn" instead of raising an error.
The current behaviour (without your patch) seems ok to me. I just wanted to point out that the patch in its current form could break on some platforms.
IMO, the current behavior is the least problematic. It also mirrors the rest of Python's posix-wrapping calls: if something usable is returned, use it; if NULL is returned, raise an error. For most people, "Unknown error XXX" or "Value error: strerror() argument out of range" won't be much different in terms of user experience. Rejecting this patch.
History
Date
User
Action
Args
2022-04-11 14:56:32
admin
set
github: 46665
2008-05-11 21:15:27
georg.brandl
set
status: open -> closedresolution: rejectedmessages: + nosy: + georg.brandl