Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more details about the Python build in sys.version #100086

Closed
vstinner opened this issue Dec 7, 2022 · 7 comments
Closed

Add more details about the Python build in sys.version #100086

vstinner opened this issue Dec 7, 2022 · 7 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@vstinner
Copy link
Member

vstinner commented Dec 7, 2022

Currently, it's not easy to guess how Python was built just by looking at python -VV output or sys.version.

I propose to enhance sys.version to include more details about how Python was configured for the build:

  • debug or release build
  • "shared" or "framework" build
  • assertions enabled or not
  • address/memory/undefined behavior sanitizer
  • etc.

It should help me to more quickly identify if a buildbot was built in debug mode or release mode.

It should help to see how Python was optimized: with or without LTO and PGO optimizations?

It should help to quickly have an idea of which ABI is used: the "pystats" ABI is different than the "debug" than the "release" ABI.

Linked PRs

@vstinner vstinner added the type-bug An unexpected behavior, bug, or error label Dec 7, 2022
@ned-deily
Copy link
Member

I don't think this change should be made. It potentially affects all users of Python, for example, the REPL users, but is of no benefit to 99% of them. All of this information is readily available via multiple other mechanisms, in particular, via python3 -m test.pythoninfo or via sysconfig.get_config_vars(). And as implemented in the PR, the information is truncated when displayed due to the string format limitation in getversion.c and can make the header unnecessarily long:

Python 3.12.0a3+ (heads/py_build_str-dirty:94b93efeed, Dec 7 2022, 16:23:18, release framework bu) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin

@vstinner
Copy link
Member Author

vstinner commented Dec 7, 2022

One motivation for me is that many users don't use Python binaries from python.org or from their Linux packaging system, but build their own binary with their custom recipe and it's not trivial to guess how the binary was built. Was it properly optimized with LTO+PGO? Was it built in debug mode?

For a long time, the most popular Docker recipe to build Python used ./configure && make: no LTO, no PGO.

Today, I cannot recall if the macOS binary is optimized with LTO or not.

Recently, @JulienPalard shared his recipe to build a custom Python with few lines of shell code: compile-python.sh. I was surprised to see --with-pydebug in the middle of the script: why building Python in debug mode? Some users may use it without paying attention to this configure flag.

in particular, via python3 -m test.pythoninfo or via sysconfig.get_config_vars()

Well, these APIs are not great :-(

test.pythoninfo is designed to be used on the command line, it doesn't have a simple API to query info about the current Python.

sysconfig.get_config_vars() is hard to use: you have to guess which variable is the best to get compiler flags: make your choice in this long list, it can depend on how Python was built, in case of doubt you might have to check multiple variables. Moreover, once you have compiler options, you have to parse this string which is not trivial (I hate parsing shell). For LTO, do you have to check compiler flags or linker flags, hum?

@vstinner
Copy link
Member Author

vstinner commented Dec 7, 2022

It potentially affects all users of Python, for example, the REPL users, but is of no benefit to 99% of them.

Ah? I'm not the 99%, I build Python on a daily basis and work on Python built in various ways. I know that :-)

I saw this "debug build" vs "release build" as an useful information, but maybe it should be omitted for the common case: "release build" string? What about "lto+pgo": is it useful for most users?

On my Fedora 37, sys.version is Python 3.11.0 (main, Oct 24 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linux.

String length:

  • Python version: 8% (6 characters)
  • space + Build info: 39% (30 characters)
  • space + compiler name and version: 53% (41 characters)
  • Total: 77 characters

To make this string shorter, the compiler part should be shorter: [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linux.

@vstinner
Copy link
Member Author

vstinner commented Dec 7, 2022

At the beginning, I started by modifying test.libregrtest to add some "build details" in the first lines of the test runner:

== CPython 3.12.0a2+ (heads/main-dirty:9dc787ea96, Dec 6 2022, 11:51:12) [GCC 9.4.0]
== Linux-5.15.0-1019-azure-x86_64-with-glibc2.31 little-endian
== cwd: /opt/buildbot/bcannon-wasm/3.x.bcannon-wasm.emscripten-node-pthreads/build/build_oot/host/build/test_python_829492æ
== CPU count: 8
== encodings: locale=UTF-8, FS=utf-8
Using random seed 7151121
0:00:00 load avg: 7.01 Run tests in parallel using 2 child processes (timeout: 15 min, worker timeout: 20 min)

These lines don't say if Python was built in debug mode or not. The test.pythonfo data is not far in CIs, but it's annoying to have to reach the test.pythoninfo step, unfold it, scroll (this is especially painful in the buildbot web UI), just to check if Python was built in debug mode or not.

The first line is coming from the code:

        print("==", platform.python_implementation(), *sys.version.split())

Since sys.version is used, I thought that maybe putting the info directly in sys.version can benefit to more users.

@vstinner
Copy link
Member Author

vstinner commented Dec 7, 2022

About the REPL, python2 and pypy put the info on two lines which makes it more readable IMO:

$ python2
Python 2.7.18 (default, Aug 22 2022, 00:00:00) 
[GCC 12.2.1 20220819 (Red Hat 12.2.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

and

$ pypy3
Python 3.9.12 (2fc6706f5902, Oct 12 2022, 21:40:58)
[PyPy 7.3.9 with GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>> 

The single line format (sys.version) is also used by the python -VV command:

$ python3 -VV
Python 3.11.0 (main, Oct 24 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)]

@vstinner
Copy link
Member Author

vstinner commented Dec 7, 2022

I wrote PR #100093 which only changes the Python test runner (libregrtest), so it doesn't change the Python REPL nor python -VV. It doesn't change sys.version, so the platform module doesn't need to be updated.

vstinner added a commit that referenced this issue Dec 8, 2022
The Python test runner (libregrtest) now logs Python build information like
"debug" vs "release" build, or LTO and PGO optimizations.
@vstinner
Copy link
Member Author

vstinner commented Dec 8, 2022

Fixed by: 3c89202

@vstinner vstinner closed this as completed Dec 8, 2022
vstinner added a commit to vstinner/cpython that referenced this issue Sep 2, 2023
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)
vstinner added a commit that referenced this issue Sep 3, 2023
…108820)

* Revert "[3.11] gh-101634: regrtest reports decoding error as failed test (#106169) (#106175)"

This reverts commit d5418e9.

* Revert "[3.11] bpo-46523: fix tests rerun when `setUp[Class|Module]` fails (GH-30895) (GH-103342)"

This reverts commit ecb09a8.

* Revert "gh-95027: Fix regrtest stdout encoding on Windows (GH-98492)"

This reverts commit b2aa28e.

* Revert "[3.11] gh-94026: Buffer regrtest worker stdout in temporary file (GH-94253) (GH-94408)"

This reverts commit 0122ab2.

* Revert "Run Tools/scripts/reindent.py (GH-94225)"

This reverts commit f0f3a42.

* Revert "gh-94052: Don't re-run failed tests with --python option (GH-94054)"

This reverts commit 1347607.

* Revert "[3.11] gh-84461: Fix Emscripten umask and permission issues (GH-94002) (GH-94006)"

This reverts commit 1073184.

* gh-93353: regrtest checks for leaked temporary files (#93776)

When running tests with -jN, create a temporary directory per process
and mark a test as "environment changed" if a test leaks a temporary
file or directory.

(cherry picked from commit e566ce5)

* gh-93353: Fix regrtest for -jN with N >= 2 (GH-93813)

(cherry picked from commit 36934a1)

* gh-93353: regrtest supports checking tmp files with -j2 (#93909)

regrtest now also implements checking for leaked temporary files and
directories when using -jN for N >= 2. Use tempfile.mkdtemp() to
create the temporary directory. Skip this check on WASI.

(cherry picked from commit 4f85cec)

* gh-84461: Fix Emscripten umask and permission issues (GH-94002)

- Emscripten's default umask is too strict, see
  emscripten-core/emscripten#17269
- getuid/getgid and geteuid/getegid are stubs that always return 0
  (root). Disable effective uid/gid syscalls and fix tests that use
  chmod() current user.
- Cannot drop X bit from directory.

(cherry picked from commit 2702e40)

* gh-94052: Don't re-run failed tests with --python option (#94054)

(cherry picked from commit 0ff7b99)

* Run Tools/scripts/reindent.py (#94225)

Reindent files which were not properly formatted (PEP 8: 4 spaces).

Remove also some trailing spaces.

(cherry picked from commit e87ada4)

* gh-94026: Buffer regrtest worker stdout in temporary file (GH-94253)

Co-authored-by: Victor Stinner <vstinner@python.org>
(cherry picked from commit 199ba23)

* gh-96465: Clear fractions hash lru_cache under refleak testing (GH-96689)

Automerge-Triggered-By: GH:zware
(cherry picked from commit 9c8f379)

* gh-95027: Fix regrtest stdout encoding on Windows (#98492)

On Windows, when the Python test suite is run with the -jN option,
the ANSI code page is now used as the encoding for the stdout
temporary file, rather than using UTF-8 which can lead to decoding
errors.

(cherry picked from commit ec1f6f5)

* gh-98903: Test suite fails with exit code 4 if no tests ran (#98904)

The Python test suite now fails wit exit code 4 if no tests ran. It
should help detecting typos in test names and test methods.

* Add "EXITCODE_" constants to Lib/test/libregrtest/main.py.
* Fix a typo: "NO TEST RUN" becomes "NO TESTS RAN"

(cherry picked from commit c76db37)

* gh-100086: Add build info to test.libregrtest (#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)

* bpo-46523: fix tests rerun when `setUp[Class|Module]` fails (#30895)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
(cherry picked from commit 9953860)

* gh-82054: allow test runner to split test_asyncio to execute in parallel by sharding. (#103927)

This runs test_asyncio sub-tests in parallel using sharding from Cinder. This suite is typically the longest-pole in runs because it is a test package with a lot of further sub-tests otherwise run serially. By breaking out the sub-tests as independent modules we can run a lot more in parallel.

After porting we can see the direct impact on a multicore system.

Without this change:
  Running make test is 5 min 26 seconds
With this change:
  Running make test takes 3 min 39 seconds

That'll vary based on system and parallelism. On a `-j 4` run similar to what CI and buildbot systems often do, it reduced the overall test suite completion latency by 10%.

The drawbacks are that this implementation is hacky and due to the sorting of the tests it obscures when the asyncio tests occur and involves changing CPython test infrastructure but, the wall time saved it is worth it, especially in low-core count CI runs as it pulls a long tail. The win for productivity and reserved CI resource usage is significant.

Future tests that deserve to be refactored into split up suites to benefit from are test_concurrent_futures and the way the _test_multiprocessing suite gets run for all start methods. As exposed by passing the -o flag to python -m test to get a list of the 10 longest running tests.

---------

Co-authored-by: Carl Meyer <carl@oddbird.net>
Co-authored-by: Gregory P. Smith <greg@krypto.org> [Google, LLC]
(cherry picked from commit 9e011e7)

* Display the sanitizer config in the regrtest header. (#105301)

Display the sanitizers present in libregrtest.

Having this in the CI output for tests with the relevant environment
variable displayed will help make it easier to do what we need to
create an equivalent local test run.

(cherry picked from commit 852348a)

* gh-101634: regrtest reports decoding error as failed test (#106169)

When running the Python test suite with -jN option, if a worker stdout
cannot be decoded from the locale encoding report a failed testn so the
exitcode is non-zero.

(cherry picked from commit 2ac3eec)

* gh-108223: test.pythoninfo and libregrtest log Py_NOGIL (#108238)

Enable with --disable-gil --without-pydebug:

    $ make pythoninfo|grep NOGIL
    sysconfig[Py_NOGIL]: 1

    $ ./python -m test
    ...
    == Python build: nogil debug
    ...

(cherry picked from commit 5afe0c1)

* gh-90791: test.pythoninfo logs ASAN_OPTIONS env var (#108289)

* Cleanup libregrtest code logging ASAN_OPTIONS.
* Fix a typo on "ASAN_OPTIONS" vs "MSAN_OPTIONS".

(cherry picked from commit 3a1ac87)

* gh-108388: regrtest splits test_asyncio package (#108393)

Currently, test_asyncio package is only splitted into sub-tests when
using command "./python -m test". With this change, it's also
splitted when passing it on the command line:
"./python -m test test_asyncio".

Remove the concept of "STDTESTS". Python is now mature enough to not
have to bother with that anymore. Removing STDTESTS simplify the
code.

(cherry picked from commit 174e9da)

* regrtest computes statistics (#108793)

test_netrc, test_pep646_syntax and test_xml_etree now return results
in the test_main() function.

Changes:

* Rewrite TestResult as a dataclass with a new State class.
* Add test.support.TestStats class and Regrtest.stats_dict attribute.
* libregrtest.runtest functions now modify a TestResult instance
  in-place.
* libregrtest summary lists the number of run tests and skipped
  tests, and denied resources.
* Add TestResult.has_meaningful_duration() method.
* Compute TestResult duration in the upper function.
* Use time.perf_counter() instead of time.monotonic().
* Regrtest: rename 'resource_denieds' attribute to 'resource_denied'.
* Rename CHILD_ERROR to MULTIPROCESSING_ERROR.
* Use match/case syntadx to have different code depending on the
  test state.

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
(cherry picked from commit d4e534c)

* gh-108822: Add Changelog entry for regrtest statistics (#108821)

---------

Co-authored-by: Christian Heimes <christian@python.org>
Co-authored-by: Zachary Ware <zach@python.org>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Joshua Herman <zitterbewegung@gmail.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants