FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-37400: pythoninfo logs getpwuid and getgrouplist (GH-14373) · DinoV/cpython@21e6cb0 · GitHub

Repository navigation

Commit 21e6cb0

Browse files
authored andcommitted
bpo-37400: pythoninfo logs getpwuid and getgrouplist (pythonGH-14373)
test.pythoninfo now also logs pwd.getpwuid(os.getuid()) and os.getgrouplist(). Extract also os.getrandom() test to run it first.
1 parent eccfe78 commit 21e6cb0

1 file changed

Lines changed: 65 additions & 30 deletions

File tree

‎Lib/test/pythoninfo.py‎

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ def collect_builtins(info_add):
161161
info_add('builtins.float.double_format', float.__getformat__("double"))
162162

163163

164+
def collect_urandom(info_add):
165+
import os
166+
167+
if hasattr(os, 'getrandom'):
168+
# PEP 524: Check if system urandom is initialized
169+
try:
170+
try:
171+
os.getrandom(1, os.GRND_NONBLOCK)
172+
state = 'ready (initialized)'
173+
except BlockingIOError as exc:
174+
state = 'not seeded yet (%s)' % exc
175+
info_add('os.getrandom', state)
176+
except OSError as exc:
177+
# Python was compiled on a more recent Linux version
178+
# than the current Linux kernel: ignore OSError(ENOSYS)
179+
if exc.errno != errno.ENOSYS:
180+
raise
181+
182+
164183
def collect_os(info_add):
165184
import os
166185

@@ -180,16 +199,16 @@ def format_attr(attr, value):
180199
)
181200
copy_attributes(info_add, os, 'os.%s', attributes, formatter=format_attr)
182201

183-
call_func(info_add, 'os.cwd', os, 'getcwd')
202+
call_func(info_add, 'os.getcwd', os, 'getcwd')
184203

185-
call_func(info_add, 'os.uid', os, 'getuid')
186-
call_func(info_add, 'os.gid', os, 'getgid')
204+
call_func(info_add, 'os.getuid', os, 'getuid')
205+
call_func(info_add, 'os.getgid', os, 'getgid')
187206
call_func(info_add, 'os.uname', os, 'uname')
188207

189208
def format_groups(groups):
190209
return ', '.join(map(str, groups))
191210

192-
call_func(info_add, 'os.groups', os, 'getgroups', formatter=format_groups)
211+
call_func(info_add, 'os.getgroups', os, 'getgroups', formatter=format_groups)
193212

194213
if hasattr(os, 'getlogin'):
195214
try:
@@ -202,7 +221,7 @@ def format_groups(groups):
202221
info_add("os.login", login)
203222

204223
call_func(info_add, 'os.cpu_count', os, 'cpu_count')
205-
call_func(info_add, 'os.loadavg', os, 'getloadavg')
224+
call_func(info_add, 'os.getloadavg', os, 'getloadavg')
206225

207226
# Environment variables used by the stdlib and tests. Don't log the full
208227
# environment: filter to list to not leak sensitive information.
@@ -286,20 +305,32 @@ def format_groups(groups):
286305
os.umask(mask)
287306
info_add("os.umask", '%03o' % mask)
288307

289-
if hasattr(os, 'getrandom'):
290-
# PEP 524: Check if system urandom is initialized
291-
try:
292-
try:
293-
os.getrandom(1, os.GRND_NONBLOCK)
294-
state = 'ready (initialized)'
295-
except BlockingIOError as exc:
296-
state = 'not seeded yet (%s)' % exc
297-
info_add('os.getrandom', state)
298-
except OSError as exc:
299-
# Python was compiled on a more recent Linux version
300-
# than the current Linux kernel: ignore OSError(ENOSYS)
301-
if exc.errno != errno.ENOSYS:
302-
raise
308+
309+
def collect_pwd(info_add):
310+
try:
311+
import pwd
312+
except ImportError:
313+
return
314+
import os
315+
316+
uid = os.getuid()
317+
try:
318+
entry = pwd.getpwuid(uid)
319+
except KeyError:
320+
entry = None
321+
322+
info_add('pwd.getpwuid(%s)'% uid,
323+
entry if entry is not None else '<KeyError>')
324+
325+
if entry is None:
326+
# there is nothing interesting to read if the current user identifier
327+
# is not the password database
328+
return
329+
330+
if hasattr(os, 'getgrouplist'):
331+
groups = os.getgrouplist(entry.pw_name, entry.pw_gid)
332+
groups = ', '.join(map(str, groups))
333+
info_add('os.getgrouplist', groups)
303334

304335

305336
def collect_readline(info_add):
@@ -625,31 +656,35 @@ def collect_info(info):
625656
info_add = info.add
626657

627658
for collect_func in (
628-
# collect_os() should be the first, to check the getrandom() status
629-
collect_os,
659+
# collect_urandom() must be the first, to check the getrandom() status.
660+
# Other functions may block on os.urandom() indirectly and so change
661+
# its state.
662+
collect_urandom,
630663

631664
collect_builtins,
665+
collect_cc,
666+
collect_datetime,
667+
collect_decimal,
668+
collect_expat,
632669
collect_gdb,
670+
collect_gdbm,
671+
collect_get_config,
633672
collect_locale,
673+
collect_os,
634674
collect_platform,
675+
collect_pwd,
635676
collect_readline,
677+
collect_resource,
636678
collect_socket,
637679
collect_sqlite,
638680
collect_ssl,
681+
collect_subprocess,
639682
collect_sys,
640683
collect_sysconfig,
684+
collect_testcapi,
641685
collect_time,
642-
collect_datetime,
643686
collect_tkinter,
644687
collect_zlib,
645-
collect_expat,
646-
collect_decimal,
647-
collect_testcapi,
648-
collect_resource,
649-
collect_cc,
650-
collect_gdbm,
651-
collect_get_config,
652-
collect_subprocess,
653688

654689
# Collecting from tests should be last as they have side effects.
655690
collect_test_socket,

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL