New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The pwd module implementation incorrectly sets some attributes to None #76214
Comments
On Android API 24: $ python -c "import pwd; print(pwd.getpwuid(0))"
pwd.struct_passwd(pw_name='root', pw_passwd='', pw_uid=0, pw_gid=0, pw_gecos=None, pw_dir='/', pw_shell='/system/bin/sh') The pw_gecos member is None and the test_values test of pwd fails because it expects a string. The fix is either (1) to skip the pw_gecos check in test_values for Android or (2) to modify the sets() function in Modules/pwdmodule.c to set an empty string instead of None when the member of the passwd structure is a NULL pointer. POSIX [1] does not specify what are the possible values of the members of the struct passwd. GNU libc states that pw_dir and pw_shell may be NULL pointers so it seems that sets() is broken in these two cases. [1] http://pubs.opengroup.org/onlinepubs/009695399/functions/getpwnam.html |
This test is wrong: it's perfectly fine to get None here. Python must not test the OS itself, but only test our own code: make sure that Python converts properly C types to nice Python types, so a string or None. I propose to use something like: def check_type(field):
self.assertTrue(field is None or isinstance(field, str), repr(field)) ... |
Hum, I changed my mind a little bit :-)
I checked the doc: pwd doesn't mention None at all :-( For practical reasons, maybe (2) is nicer option. It would avoid to have all existing code just for Android. I'm not sure that it's very useful to distinguish NULL and an empty char* string. |
I agree. An attribute of a ('pwd' Python module) password database entry corresponds to the field of a line in a 'passwd' text file. So it makes sense that when the field is empty in the text file, the corresponding attribute be an empty string and never None if it is not an integer (FWIW Android does not have a 'passwd' file). Changing the title of the issue. |
I disagree. This is an old API, a thin wrapper around standard POSIX API, and returning an empty string instead of None will make impossible to distinguish NULL from "". It is easy to convert None in an empty string in Python: I would change the test to if field is not None:
self.assertIsInstance(field, str) or self.assertIsInstance(field, (str, type(None))) (I prefer the former variant). |
Changing test_pwd does not correct the fact that the current implementation of the pwd module may break an existing Python application since this (old indeed) API states "The uid and gid items are integers, all others are strings".
AFAIK in the 50 years since the creation of the unix operating system, there has never been an implementation of pwd that states that a string field may be either an empty string or NULL. And it is doubtful that there will ever be one, since this would break all (all, not just the Python applications) existing applications using pwd. |
On your second link it is documented explicitly that pw_dir and pw_shell might be NULL. And at least for pw_shell the behavior for NULL and "" are different. |
What is the difference between the two? |
It's now fixed in master and backports to 3.7 and 3.8 will be merged as soon as the CI pass. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: