]> granicus.if.org Git - python/commitdiff
[3.6] bpo-30871: Add test.pythoninfo (#3174)
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 21 Aug 2017 22:17:15 +0000 (00:17 +0200)
committerGitHub <noreply@github.com>
Mon, 21 Aug 2017 22:17:15 +0000 (00:17 +0200)
* bpo-30871: Add test.pythoninfo (#3075)

* Add Lib/test/pythoninfo.py: script collecting various informations
  about Python to help debugging test failures.
* regrtest: remove sys.hash_info and sys.flags from header.
* Travis CI, Appveyor: run pythoninfo before tests
(cherry picked from commit b907abc88589f7bea52c5afe172ececc6edcda70)

* bpo-30871: pythoninfo: add expat and _decimal (#3121)

* bpo-30871: pythoninfo: add expat and _decimal

* Remove _decimal.__version__

The string is hardcoded, not really interesting.

(cherry picked from commit f6ebd838f00b4c211c72d85ee49749e910cd3afe)

* bpo-30871: Add "make pythoninfo" (#3120)

(cherry picked from commit a3a01a2fceab2188b282ab9911f79c99a4c32273)

* bpo-30871: pythoninfo: more sys, os, time data (#3130)

* bpo-30871: pythoninfo: more sys, os, time data

PythonInfo now converts types other than intger to string by default.

* fix typo

(cherry picked from commit ad7eaed54382b346784e51a6f0122ce81e8842b5)

* bpo-31231: Fix pythoninfo in Travis config (#3134)

bpo-31231, bpo-30871: Replace "./python -m test.pythoninfo" with
"make pythoninfo", since macOS uses ./python.exe.
(cherry picked from commit 92b1f90143286385c0ff5be98d3721b90580a912)

.github/appveyor.yml
.travis.yml
Lib/test/libregrtest/main.py
Lib/test/pythoninfo.py [new file with mode: 0644]
Makefile.pre.in

index 38ca3c4de1501c43d80195f427fd52f625a1f3a9..aa6c3ab88cc078c991f3e6a75276757817ab6024 100644 (file)
@@ -7,6 +7,7 @@ branches:
     - buildbot-custom
 build_script:
 - cmd: PCbuild\build.bat -e
+- cmd: PCbuild\win32\python.exe -m test.pythoninfo
 test_script:
 - cmd: PCbuild\rt.bat -q -uall -u-cpu -rwW --slowest --timeout=1200 -j0
 environment:
index cf59d965a871d66357b497d534fb81868a7f147b..57b4c08a3e78e2a293f781923ceadfa998d3dd8e 100644 (file)
@@ -59,6 +59,7 @@ matrix:
             # Need a venv that can parse covered code.
             ./python -m venv venv
             ./venv/bin/python -m pip install -U coverage
+            ./venv/bin/python -m test.pythoninfo
       script:
         # Skip tests that re-run the entire test suite.
         - ./venv/bin/python -m coverage run --pylib -m test -uall,-cpu -x test_multiprocessing_fork -x test_multiprocessing_forkserver -x test_multiprocessing_spawn
@@ -86,6 +87,7 @@ before_script:
         echo "$changes"
         exit 1
       fi
+      make pythoninfo
 
 script:
   # Using the built Python as patchcheck.py is built around the idea of using
index bc8155bbff2b478a5de50ef84ee55e9dc84753ee..d44f69dda2f70ed3c6b1f5c38a435d7fcb2dbdbb 100644 (file)
@@ -423,8 +423,6 @@ class Regrtest:
         print("==", platform.python_implementation(), *sys.version.split())
         print("==", platform.platform(aliased=True),
                       "%s-endian" % sys.byteorder)
-        print("== hash algorithm:", sys.hash_info.algorithm,
-              "64bit" if sys.maxsize > 2**32 else "32bit")
         print("== cwd:", os.getcwd())
         cpu_count = os.cpu_count()
         if cpu_count:
@@ -432,7 +430,6 @@ class Regrtest:
         print("== encodings: locale=%s, FS=%s"
               % (locale.getpreferredencoding(False),
                  sys.getfilesystemencoding()))
-        print("Testing with flags:", sys.flags)
 
     def run_tests(self):
         # For a partial run, we do not need to clutter the output.
diff --git a/Lib/test/pythoninfo.py b/Lib/test/pythoninfo.py
new file mode 100644 (file)
index 0000000..15cce34
--- /dev/null
@@ -0,0 +1,471 @@
+"""
+Collect various informations about Python to help debugging test failures.
+"""
+from __future__ import print_function
+import re
+import sys
+import traceback
+
+
+def normalize_text(text):
+    if text is None:
+        return None
+    text = str(text)
+    text = re.sub(r'\s+', ' ', text)
+    return text.strip()
+
+
+class PythonInfo:
+    def __init__(self):
+        self.info = {}
+
+    def add(self, key, value):
+        if key in self.info:
+            raise ValueError("duplicate key: %r" % key)
+
+        if value is None:
+            return
+
+        if not isinstance(value, int):
+            if not isinstance(value, str):
+                # convert other objects like sys.flags to string
+                value = str(value)
+
+            value = value.strip()
+            if not value:
+                return
+
+        self.info[key] = value
+
+    def get_infos(self):
+        """
+        Get informations as a key:value dictionary where values are strings.
+        """
+        return {key: str(value) for key, value in self.info.items()}
+
+
+def copy_attributes(info_add, obj, name_fmt, attributes, *, formatter=None):
+    for attr in attributes:
+        value = getattr(obj, attr, None)
+        if value is None:
+            continue
+        name = name_fmt % attr
+        if formatter is not None:
+            value = formatter(attr, value)
+        info_add(name, value)
+
+
+def call_func(info_add, name, mod, func_name, *, formatter=None):
+    try:
+        func = getattr(mod, func_name)
+    except AttributeError:
+        return
+    value = func()
+    if formatter is not None:
+        value = formatter(value)
+    info_add(name, value)
+
+
+def collect_sys(info_add):
+    attributes = (
+        '_framework',
+        'abiflags',
+        'api_version',
+        'builtin_module_names',
+        'byteorder',
+        'dont_write_bytecode',
+        'executable',
+        'flags',
+        'float_info',
+        'float_repr_style',
+        'hash_info',
+        'hexversion',
+        'implementation',
+        'int_info',
+        'maxsize',
+        'maxunicode',
+        'path',
+        'platform',
+        'prefix',
+        'thread_info',
+        'version',
+        'version_info',
+        'winver',
+    )
+    copy_attributes(info_add, sys, 'sys.%s', attributes)
+
+    call_func(info_add, 'sys.androidapilevel', sys, 'getandroidapilevel')
+    call_func(info_add, 'sys.windowsversion', sys, 'getwindowsversion')
+
+    encoding = sys.getfilesystemencoding()
+    if hasattr(sys, 'getfilesystemencodeerrors'):
+        encoding = '%s/%s' % (encoding, sys.getfilesystemencodeerrors())
+    info_add('sys.filesystem_encoding', encoding)
+
+    for name in ('stdin', 'stdout', 'stderr'):
+        stream = getattr(sys, name)
+        if stream is None:
+            continue
+        encoding = getattr(stream, 'encoding', None)
+        if not encoding:
+            continue
+        errors = getattr(stream, 'errors', None)
+        if errors:
+            encoding = '%s/%s' % (encoding, errors)
+        info_add('sys.%s.encoding' % name, encoding)
+
+
+def collect_platform(info_add):
+    import platform
+
+    arch = platform.architecture()
+    arch = ' '.join(filter(bool, arch))
+    info_add('platform.architecture', arch)
+
+    info_add('platform.python_implementation',
+             platform.python_implementation())
+    info_add('platform.platform',
+             platform.platform(aliased=True))
+
+
+def collect_locale(info_add):
+    import locale
+
+    info_add('locale.encoding', locale.getpreferredencoding(False))
+
+
+def collect_os(info_add):
+    import os
+
+    def format_attr(attr, value):
+        if attr in ('supports_follow_symlinks', 'supports_fd',
+                    'supports_effective_ids'):
+            return str(sorted(func.__name__ for func in value))
+        else:
+            return value
+
+    attributes = (
+        'name',
+        'supports_bytes_environ',
+        'supports_effective_ids',
+        'supports_fd',
+        'supports_follow_symlinks',
+    )
+    copy_attributes(info_add, os, 'os.%s', attributes, formatter=format_attr)
+
+    info_add("os.cwd", os.getcwd())
+
+    call_func(info_add, 'os.uid', os, 'getuid')
+    call_func(info_add, 'os.gid', os, 'getgid')
+    call_func(info_add, 'os.uname', os, 'uname')
+
+    if hasattr(os, 'getgroups'):
+        groups = os.getgroups()
+        groups = map(str, groups)
+        groups = ', '.join(groups)
+        info_add("os.groups", groups)
+
+    if hasattr(os, 'getlogin'):
+        try:
+            login = os.getlogin()
+        except OSError:
+            # getlogin() fails with "OSError: [Errno 25] Inappropriate ioctl
+            # for device" on Travis CI
+            pass
+        else:
+            info_add("os.login", login)
+
+    if hasattr(os, 'cpu_count'):
+        cpu_count = os.cpu_count()
+        if cpu_count:
+            info_add('os.cpu_count', cpu_count)
+
+    call_func(info_add, 'os.loadavg', os, 'getloadavg')
+
+    # Get environment variables: filter to list
+    # to not leak sensitive information
+    ENV_VARS = (
+        "CC",
+        "COMSPEC",
+        "DISPLAY",
+        "DISTUTILS_USE_SDK",
+        "DYLD_LIBRARY_PATH",
+        "HOME",
+        "HOMEDRIVE",
+        "HOMEPATH",
+        "LANG",
+        "LD_LIBRARY_PATH",
+        "MACOSX_DEPLOYMENT_TARGET",
+        "MAKEFLAGS",
+        "MSSDK",
+        "PATH",
+        "SDK_TOOLS_BIN",
+        "SHELL",
+        "TEMP",
+        "TERM",
+        "TMP",
+        "TMPDIR",
+        "USERPROFILE",
+        "WAYLAND_DISPLAY",
+    )
+    for name, value in os.environ.items():
+        uname = name.upper()
+        if (uname in ENV_VARS or uname.startswith(("PYTHON", "LC_"))
+           # Visual Studio: VS140COMNTOOLS
+           or (uname.startswith("VS") and uname.endswith("COMNTOOLS"))):
+            info_add('os.environ[%s]' % name, value)
+
+    if hasattr(os, 'umask'):
+        mask = os.umask(0)
+        os.umask(mask)
+        info_add("os.umask", '%03o' % mask)
+
+    if hasattr(os, 'getrandom'):
+        # PEP 524: Check if system urandom is initialized
+        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)
+
+
+def collect_readline(info_add):
+    try:
+        import readline
+    except ImportError:
+        return
+
+    def format_attr(attr, value):
+        if isinstance(value, int):
+            return "%#x" % value
+        else:
+            return value
+
+    attributes = (
+        "_READLINE_VERSION",
+        "_READLINE_RUNTIME_VERSION",
+        "_READLINE_LIBRARY_VERSION",
+    )
+    copy_attributes(info_add, readline, 'readline.%s', attributes,
+                    formatter=format_attr)
+
+
+def collect_gdb(info_add):
+    import subprocess
+
+    try:
+        proc = subprocess.Popen(["gdb", "-nx", "--version"],
+                                stdout=subprocess.PIPE,
+                                stderr=subprocess.PIPE,
+                                universal_newlines=True)
+        version = proc.communicate()[0]
+    except OSError:
+        return
+
+    # Only keep the first line
+    version = version.splitlines()[0]
+    info_add('gdb_version', version)
+
+
+def collect_tkinter(info_add):
+    try:
+        import _tkinter
+    except ImportError:
+        pass
+    else:
+        attributes = ('TK_VERSION', 'TCL_VERSION')
+        copy_attributes(info_add, _tkinter, 'tkinter.%s', attributes)
+
+    try:
+        import tkinter
+    except ImportError:
+        pass
+    else:
+        tcl = tkinter.Tcl()
+        patchlevel = tcl.call('info', 'patchlevel')
+        info_add('tkinter.info_patchlevel', patchlevel)
+
+
+def collect_time(info_add):
+    import time
+
+    attributes = (
+        'altzone',
+        'daylight',
+        'timezone',
+        'tzname',
+    )
+    copy_attributes(info_add, time, 'time.%s', attributes)
+
+    if not hasattr(time, 'get_clock_info'):
+        return
+
+    for clock in ('time', 'perf_counter'):
+        tinfo = time.get_clock_info(clock)
+        info_add('time.%s' % clock, tinfo)
+
+
+def collect_sysconfig(info_add):
+    import sysconfig
+
+    for name in (
+        'ABIFLAGS',
+        'ANDROID_API_LEVEL',
+        'CC',
+        'CCSHARED',
+        'CFLAGS',
+        'CFLAGSFORSHARED',
+        'PY_LDFLAGS',
+        'CONFIG_ARGS',
+        'HOST_GNU_TYPE',
+        'MACHDEP',
+        'MULTIARCH',
+        'OPT',
+        'PY_CFLAGS',
+        'PY_CFLAGS_NODIST',
+        'Py_DEBUG',
+        'Py_ENABLE_SHARED',
+        'SHELL',
+        'SOABI',
+        'prefix',
+    ):
+        value = sysconfig.get_config_var(name)
+        if name == 'ANDROID_API_LEVEL' and not value:
+            # skip ANDROID_API_LEVEL=0
+            continue
+        value = normalize_text(value)
+        info_add('sysconfig[%s]' % name, value)
+
+
+def collect_ssl(info_add):
+    try:
+        import ssl
+    except ImportError:
+        return
+
+    def format_attr(attr, value):
+        if attr.startswith('OP_'):
+            return '%#8x' % value
+        else:
+            return value
+
+    attributes = (
+        'OPENSSL_VERSION',
+        'OPENSSL_VERSION_INFO',
+        'HAS_SNI',
+        'OP_ALL',
+        'OP_NO_TLSv1_1',
+    )
+    copy_attributes(info_add, ssl, 'ssl.%s', attributes, formatter=format_attr)
+
+
+def collect_socket(info_add):
+    import socket
+
+    hostname = socket.gethostname()
+    info_add('socket.hostname', hostname)
+
+
+def collect_sqlite(info_add):
+    try:
+        import sqlite3
+    except ImportError:
+        return
+
+    attributes = ('version', 'sqlite_version')
+    copy_attributes(info_add, sqlite3, 'sqlite3.%s', attributes)
+
+
+def collect_zlib(info_add):
+    try:
+        import zlib
+    except ImportError:
+        return
+
+    attributes = ('ZLIB_VERSION', 'ZLIB_RUNTIME_VERSION')
+    copy_attributes(info_add, zlib, 'zlib.%s', attributes)
+
+
+def collect_expat(info_add):
+    try:
+        from xml.parsers import expat
+    except ImportError:
+        return
+
+    attributes = ('EXPAT_VERSION',)
+    copy_attributes(info_add, expat, 'expat.%s', attributes)
+
+
+def collect_decimal(info_add):
+    try:
+        import _decimal
+    except ImportError:
+        return
+
+    attributes = ('__libmpdec_version__',)
+    copy_attributes(info_add, _decimal, '_decimal.%s', attributes)
+
+
+def collect_info(info):
+    error = False
+    info_add = info.add
+
+    for collect_func in (
+        # collect_os() should be the first, to check the getrandom() status
+        collect_os,
+
+        collect_gdb,
+        collect_locale,
+        collect_platform,
+        collect_readline,
+        collect_socket,
+        collect_sqlite,
+        collect_ssl,
+        collect_sys,
+        collect_sysconfig,
+        collect_time,
+        collect_tkinter,
+        collect_zlib,
+        collect_expat,
+        collect_decimal,
+    ):
+        try:
+            collect_func(info_add)
+        except Exception as exc:
+            error = True
+            print("ERROR: %s() failed" % (collect_func.__name__),
+                  file=sys.stderr)
+            traceback.print_exc(file=sys.stderr)
+            print(file=sys.stderr)
+            sys.stderr.flush()
+
+    return error
+
+
+def dump_info(info, file=None):
+    title = "Python debug information"
+    print(title)
+    print("=" * len(title))
+    print()
+
+    infos = info.get_infos()
+    infos = sorted(infos.items())
+    for key, value in infos:
+        value = value.replace("\n", " ")
+        print("%s: %s" % (key, value))
+    print()
+
+
+def main():
+    info = PythonInfo()
+    error = collect_info(info)
+    dump_info(info)
+
+    if error:
+        print("Collection failed: exit with error", file=sys.stderr)
+        sys.exit(1)
+
+
+if __name__ == "__main__":
+    main()
index 93a2bab6d983fe78634b7b093dc481661f9cb49f..0a8ac713017772ab9ea8e8b97e1bbf31dacbe838 100644 (file)
@@ -1001,6 +1001,8 @@ TESTPYTHON=       $(RUNSHARED) ./$(BUILDPYTHON) $(TESTPYTHONOPTS)
 TESTRUNNER=    $(TESTPYTHON) $(srcdir)/Tools/scripts/run_tests.py
 TESTTIMEOUT=   1200
 
+.PHONY: test testall testuniversal buildbottest pythoninfo
+
 # Run a basic set of regression tests.
 # This excludes some tests that are particularly resource-intensive.
 test:          @DEF_MAKE_RULE@ platform
@@ -1039,6 +1041,9 @@ buildbottest:     build_all platform
                fi
                $(TESTRUNNER) -j 1 -u all -W --slowest --timeout=$(TESTTIMEOUT) $(TESTOPTS)
 
+pythoninfo: build_all
+               $(RUNSHARED) ./$(BUILDPYTHON) -m test.pythoninfo
+
 QUICKTESTOPTS= $(TESTOPTS) -x test_subprocess test_io test_lib2to3 \
                test_multibytecodec test_urllib2_localnet test_itertools \
                test_multiprocessing_fork test_multiprocessing_spawn \