Why is there a different behavior from getpwuid and getgrgid?

In Python 2.7, 3.4 and 3.5, grp.getgrgid is able to accept a string:

 from grp import getgrgid print(getgrgid('0')) 

However, pwd.getpwuid cannot do the same:

 from pwd import getpwuid print(getpwuid('0')) Traceback (most recent call last): File "getpwuid_test.py", line 2, in <module> print(getpwuid('0')) TypeError: an integer is required 

This is because inside Modules/pwdmodule.c , getpwuid uses PyNumber_ParseTuple with a converter that uses PyNumber_Index to get a Python integer and throws an exception on error.

However, in Modules/grpmodule.c , grp_getgrgid first uses PyNumber_Long (Or PyNumber_Int for the old Python is enough) as a conversion, and as the documentation says, https://docs.python.org/3/c-api/number. html is the equivalent of running int(o) , which can convert a string to an integer. Only then is this given by PyNumber_Index , through the _Py_Gid_Converter helper function

What is the reason for this difference? Is this a deliberate choice based on some kind of story?

The behavior of getgrgid seems more useful, and it is strange that it does not apply to both functions. Is this unwanted behavior in getgrgid or getpwuid ?

+7
python cpython
source share
1 answer

The short answer is that people make mistakes and mistakes happen. Some of them are doozies and are quickly spotted , others are minor and can go on for years without detection.

Thanks for helping make Python better!

0
source share

All Articles