summaryrefslogtreecommitdiff
path: root/Lib/platform.py
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2018-12-05 14:04:52 +0100
committerGitHub <noreply@github.com>2018-12-05 14:04:52 +0100
commit476b113ed8531b9fbb0bd023a05eb3af21996600 (patch)
tree820e7430ff8bcbcd4ed46cde4fcb6ac487e0e7eb /Lib/platform.py
parent2a893430c9c8378cbdfac95895a64fa07aaff9ed (diff)
downloadcpython-git-476b113ed8531b9fbb0bd023a05eb3af21996600.tar.gz
bpo-35389: platform.libc_ver() uses os.confstr() (GH-10891)
platform.libc_ver() now uses os.confstr('CS_GNU_LIBC_VERSION') if available and the *executable* parameter is not set. The default value of the libc_ver() *executable* parameter becomes None. Quick benchmark on Fedora 29: python3 -m perf command ./python -S -c 'import platform; platform.libc_ver()' 94.9 ms +- 4.3 ms -> 33.2 ms +- 1.4 ms: 2.86x faster (-65%)
Diffstat (limited to 'Lib/platform.py')
-rwxr-xr-xLib/platform.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/Lib/platform.py b/Lib/platform.py
index 74346c4cab..f089a463ef 100755
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -169,7 +169,7 @@ _libc_search = re.compile(b'(__libc_init)'
b'|'
br'(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)', re.ASCII)
-def libc_ver(executable=sys.executable, lib='', version='', chunksize=16384):
+def libc_ver(executable=None, lib='', version='', chunksize=16384):
""" Tries to determine the libc version that the file executable
(which defaults to the Python interpreter) is linked against.
@@ -184,6 +184,19 @@ def libc_ver(executable=sys.executable, lib='', version='', chunksize=16384):
The file is read and scanned in chunks of chunksize bytes.
"""
+ if executable is None:
+ try:
+ ver = os.confstr('CS_GNU_LIBC_VERSION')
+ # parse 'glibc 2.28' as ('glibc', '2.28')
+ parts = ver.split(maxsplit=1)
+ if len(parts) == 2:
+ return tuple(parts)
+ except (AttributeError, ValueError, OSError):
+ # os.confstr() or CS_GNU_LIBC_VERSION value not available
+ pass
+
+ executable = sys.executable
+
V = _comparable_version
if hasattr(os.path, 'realpath'):
# Python 2.2 introduced os.path.realpath(); it is used