Skip to content

Commit c5bfed5

Browse files
committed
pythongh-100086: Add build info to test.libregrtest (python#100093)
The Python test runner (libregrtest) now logs Python build information like "debug" vs "release" build, or LTO and PGO optimizations. (cherry picked from commit 3c89202)
1 parent 2cc75b6 commit c5bfed5

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

Lib/test/libregrtest/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
ChildError, DidNotRun)
1818
from test.libregrtest.setup import setup_tests
1919
from test.libregrtest.pgo import setup_pgo_tests
20-
from test.libregrtest.utils import removepy, count, format_duration, printlist
20+
from test.libregrtest.utils import (removepy, count, format_duration,
21+
printlist, get_build_info)
2122
from test import support
2223
from test.support import os_helper
2324
from test.support import threading_helper
@@ -491,6 +492,7 @@ def display_header(self):
491492
print("==", platform.python_implementation(), *sys.version.split())
492493
print("==", platform.platform(aliased=True),
493494
"%s-endian" % sys.byteorder)
495+
print("== Python build:", ' '.join(get_build_info()))
494496
print("== cwd:", os.getcwd())
495497
cpu_count = os.cpu_count()
496498
if cpu_count:

Lib/test/libregrtest/utils.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import math
22
import os.path
33
import sys
4+
import sysconfig
45
import textwrap
56
from test import support
67

@@ -217,3 +218,87 @@ def clear_caches():
217218
pass
218219
else:
219220
fractions._hash_algorithm.cache_clear()
221+
222+
223+
def get_build_info():
224+
# Get most important configure and build options as a list of strings.
225+
# Example: ['debug', 'ASAN+MSAN'] or ['release', 'LTO+PGO'].
226+
227+
config_args = sysconfig.get_config_var('CONFIG_ARGS') or ''
228+
cflags = sysconfig.get_config_var('PY_CFLAGS') or ''
229+
cflags_nodist = sysconfig.get_config_var('PY_CFLAGS_NODIST') or ''
230+
ldflags_nodist = sysconfig.get_config_var('PY_LDFLAGS_NODIST') or ''
231+
232+
build = []
233+
if hasattr(sys, 'gettotalrefcount'):
234+
# --with-pydebug
235+
build.append('debug')
236+
237+
if '-DNDEBUG' in (cflags + cflags_nodist):
238+
build.append('without_assert')
239+
else:
240+
build.append('release')
241+
242+
if '--with-assertions' in config_args:
243+
build.append('with_assert')
244+
elif '-DNDEBUG' not in (cflags + cflags_nodist):
245+
build.append('with_assert')
246+
247+
# --enable-framework=name
248+
framework = sysconfig.get_config_var('PYTHONFRAMEWORK')
249+
if framework:
250+
build.append(f'framework={framework}')
251+
252+
# --enable-shared
253+
shared = int(sysconfig.get_config_var('PY_ENABLE_SHARED') or '0')
254+
if shared:
255+
build.append('shared')
256+
257+
# --with-lto
258+
optimizations = []
259+
if '-flto=thin' in ldflags_nodist:
260+
optimizations.append('ThinLTO')
261+
elif '-flto' in ldflags_nodist:
262+
optimizations.append('LTO')
263+
264+
# --enable-optimizations
265+
pgo_options = (
266+
# GCC
267+
'-fprofile-use',
268+
# clang: -fprofile-instr-use=code.profclangd
269+
'-fprofile-instr-use',
270+
# ICC
271+
"-prof-use",
272+
)
273+
if any(option in cflags_nodist for option in pgo_options):
274+
optimizations.append('PGO')
275+
if optimizations:
276+
build.append('+'.join(optimizations))
277+
278+
# --with-address-sanitizer
279+
sanitizers = []
280+
if support.check_sanitizer(address=True):
281+
sanitizers.append("ASAN")
282+
# --with-memory-sanitizer
283+
if support.check_sanitizer(memory=True):
284+
sanitizers.append("MSAN")
285+
# --with-undefined-behavior-sanitizer
286+
if support.check_sanitizer(ub=True):
287+
sanitizers.append("UBSAN")
288+
if sanitizers:
289+
build.append('+'.join(sanitizers))
290+
291+
# --with-trace-refs
292+
if hasattr(sys, 'getobjects'):
293+
build.append("TraceRefs")
294+
# --enable-pystats
295+
if hasattr(sys, '_stats_on'):
296+
build.append("pystats")
297+
# --with-valgrind
298+
if sysconfig.get_config_var('WITH_VALGRIND'):
299+
build.append("valgrind")
300+
# --with-dtrace
301+
if sysconfig.get_config_var('WITH_DTRACE'):
302+
build.append("dtrace")
303+
304+
return build
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The Python test runner (libregrtest) now logs Python build information like
2+
"debug" vs "release" build, or LTO and PGO optimizations. Patch by Victor
3+
Stinner.

0 commit comments

Comments
 (0)