Commit 9cb27411 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-37400: pythoninfo logs getpwuid and getgrouplist (GH-14373)

test.pythoninfo now also logs pwd.getpwuid(os.getuid()) and
os.getgrouplist(). Extract also os.getrandom() test to run it first.
parent de9b606c
...@@ -161,6 +161,25 @@ def collect_builtins(info_add): ...@@ -161,6 +161,25 @@ def collect_builtins(info_add):
info_add('builtins.float.double_format', float.__getformat__("double")) info_add('builtins.float.double_format', float.__getformat__("double"))
def collect_urandom(info_add):
import os
if hasattr(os, 'getrandom'):
# PEP 524: Check if system urandom is initialized
try:
try:
os.getrandom(1, os.GRND_NONBLOCK)
state = 'ready (initialized)'
except BlockingIOError as exc:
state = 'not seeded yet (%s)' % exc
info_add('os.getrandom', state)
except OSError as exc:
# Python was compiled on a more recent Linux version
# than the current Linux kernel: ignore OSError(ENOSYS)
if exc.errno != errno.ENOSYS:
raise
def collect_os(info_add): def collect_os(info_add):
import os import os
...@@ -180,16 +199,16 @@ def collect_os(info_add): ...@@ -180,16 +199,16 @@ def collect_os(info_add):
) )
copy_attributes(info_add, os, 'os.%s', attributes, formatter=format_attr) copy_attributes(info_add, os, 'os.%s', attributes, formatter=format_attr)
call_func(info_add, 'os.cwd', os, 'getcwd') call_func(info_add, 'os.getcwd', os, 'getcwd')
call_func(info_add, 'os.uid', os, 'getuid') call_func(info_add, 'os.getuid', os, 'getuid')
call_func(info_add, 'os.gid', os, 'getgid') call_func(info_add, 'os.getgid', os, 'getgid')
call_func(info_add, 'os.uname', os, 'uname') call_func(info_add, 'os.uname', os, 'uname')
def format_groups(groups): def format_groups(groups):
return ', '.join(map(str, groups)) return ', '.join(map(str, groups))
call_func(info_add, 'os.groups', os, 'getgroups', formatter=format_groups) call_func(info_add, 'os.getgroups', os, 'getgroups', formatter=format_groups)
if hasattr(os, 'getlogin'): if hasattr(os, 'getlogin'):
try: try:
...@@ -202,7 +221,7 @@ def collect_os(info_add): ...@@ -202,7 +221,7 @@ def collect_os(info_add):
info_add("os.login", login) info_add("os.login", login)
call_func(info_add, 'os.cpu_count', os, 'cpu_count') call_func(info_add, 'os.cpu_count', os, 'cpu_count')
call_func(info_add, 'os.loadavg', os, 'getloadavg') call_func(info_add, 'os.getloadavg', os, 'getloadavg')
# Environment variables used by the stdlib and tests. Don't log the full # Environment variables used by the stdlib and tests. Don't log the full
# environment: filter to list to not leak sensitive information. # environment: filter to list to not leak sensitive information.
...@@ -286,20 +305,32 @@ def collect_os(info_add): ...@@ -286,20 +305,32 @@ def collect_os(info_add):
os.umask(mask) os.umask(mask)
info_add("os.umask", '%03o' % mask) info_add("os.umask", '%03o' % mask)
if hasattr(os, 'getrandom'):
# PEP 524: Check if system urandom is initialized def collect_pwd(info_add):
try: try:
import pwd
except ImportError:
return
import os
uid = os.getuid()
try: try:
os.getrandom(1, os.GRND_NONBLOCK) entry = pwd.getpwuid(uid)
state = 'ready (initialized)' except KeyError:
except BlockingIOError as exc: entry = None
state = 'not seeded yet (%s)' % exc
info_add('os.getrandom', state) info_add('pwd.getpwuid(%s)'% uid,
except OSError as exc: entry if entry is not None else '<KeyError>')
# Python was compiled on a more recent Linux version
# than the current Linux kernel: ignore OSError(ENOSYS) if entry is None:
if exc.errno != errno.ENOSYS: # there is nothing interesting to read if the current user identifier
raise # is not the password database
return
if hasattr(os, 'getgrouplist'):
groups = os.getgrouplist(entry.pw_name, entry.pw_gid)
groups = ', '.join(map(str, groups))
info_add('os.getgrouplist', groups)
def collect_readline(info_add): def collect_readline(info_add):
...@@ -625,31 +656,35 @@ def collect_info(info): ...@@ -625,31 +656,35 @@ def collect_info(info):
info_add = info.add info_add = info.add
for collect_func in ( for collect_func in (
# collect_os() should be the first, to check the getrandom() status # collect_urandom() must be the first, to check the getrandom() status.
collect_os, # Other functions may block on os.urandom() indirectly and so change
# its state.
collect_urandom,
collect_builtins, collect_builtins,
collect_cc,
collect_datetime,
collect_decimal,
collect_expat,
collect_gdb, collect_gdb,
collect_gdbm,
collect_get_config,
collect_locale, collect_locale,
collect_os,
collect_platform, collect_platform,
collect_pwd,
collect_readline, collect_readline,
collect_resource,
collect_socket, collect_socket,
collect_sqlite, collect_sqlite,
collect_ssl, collect_ssl,
collect_subprocess,
collect_sys, collect_sys,
collect_sysconfig, collect_sysconfig,
collect_testcapi,
collect_time, collect_time,
collect_datetime,
collect_tkinter, collect_tkinter,
collect_zlib, collect_zlib,
collect_expat,
collect_decimal,
collect_testcapi,
collect_resource,
collect_cc,
collect_gdbm,
collect_get_config,
collect_subprocess,
# Collecting from tests should be last as they have side effects. # Collecting from tests should be last as they have side effects.
collect_test_socket, collect_test_socket,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment