grp.struct_group bug ?

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Wed Jan 31 00:34:54 EST 2007


On Jan 30, 5:42 pm, spam <x... at x.com> wrote:
> Is this a bug ?
>
> Running the following script with Python 2.3.5:
> ................
> #!/usr/bin/python
>
> import grp
>
> # groups = grp.getgrall()
>
> agroup = grp.getgrnam('wheel')
> print agroup
> print type(agroup)
>
> print agroup.__contains__('john')
> print agroup.__contains__('x')
> ................
>
> This is the output I get:
> ................
> ('wheel', 'x', 199, ['marcel', 'john', 'ben'])
> <type 'grp.struct_group'>
> False
> True
> ................
>
> I expected __contains__ to tell me if a given user is part of a group, but
> it seems that it's checking the password field ??



The tuple returned by getgrnam contains four fields:
the group id (gr_gid), a list of group members (gr_grmem),
the group name (gr_name), and the group password (gr_passwd).

If you want to see if user 'john' is in the list of group members,
use:

if 'john' in agroup.gr_mem:
    print "Found john in group wheel"

If you want to see if a password was set:

if agroup.gr_passwd == "":
    print "No group password was set for group wheel"

and so on.

--
Hope this helps,
Steven





More information about the Python-list mailing list