Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 65 additions & 30 deletions Lib/test/pythoninfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@ def collect_builtins(info_add):
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):
import os

Expand All @@ -180,16 +199,16 @@ def format_attr(attr, value):
)
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.gid', os, 'getgid')
call_func(info_add, 'os.getuid', os, 'getuid')
call_func(info_add, 'os.getgid', os, 'getgid')
call_func(info_add, 'os.uname', os, 'uname')

def format_groups(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'):
try:
Expand All @@ -202,7 +221,7 @@ def format_groups(groups):
info_add("os.login", login)

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: filter to list to not leak sensitive information.
Expand Down Expand Up @@ -286,20 +305,32 @@ def format_groups(groups):
os.umask(mask)
info_add("os.umask", '%03o' % mask)

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_pwd(info_add):
try:
import pwd
except ImportError:
return
import os

uid = os.getuid()
try:
entry = pwd.getpwuid(uid)
except KeyError:
entry = None

info_add('pwd.getpwuid(%s)'% uid,
entry if entry is not None else '<KeyError>')

if entry is None:
# there is nothing interesting to read if the current user identifier
# 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):
Expand Down Expand Up @@ -625,31 +656,35 @@ def collect_info(info):
info_add = info.add

for collect_func in (
# collect_os() should be the first, to check the getrandom() status
collect_os,
# collect_urandom() must be the first, to check the getrandom() status.
# Other functions may block on os.urandom() indirectly and so change
# its state.
collect_urandom,

collect_builtins,
collect_cc,
collect_datetime,
collect_decimal,
collect_expat,
collect_gdb,
collect_gdbm,
collect_get_config,
collect_locale,
collect_os,
collect_platform,
collect_pwd,
collect_readline,
collect_resource,
collect_socket,
collect_sqlite,
collect_ssl,
collect_subprocess,
collect_sys,
collect_sysconfig,
collect_testcapi,
collect_time,
collect_datetime,
collect_tkinter,
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.
collect_test_socket,
Expand Down