Make platform.libc_ver() less slow

This commit is contained in:
Antoine Pitrou 2011-10-07 13:26:59 +02:00
parent 978b9d2a27
commit ba7c226095

View file

@ -130,15 +130,15 @@ except AttributeError:
### Platform specific APIs ### Platform specific APIs
_libc_search = re.compile(r'(__libc_init)' _libc_search = re.compile(b'(__libc_init)'
'|' b'|'
'(GLIBC_([0-9.]+))' b'(GLIBC_([0-9.]+))'
'|' b'|'
'(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)', re.ASCII) br'(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)', re.ASCII)
def libc_ver(executable=sys.executable,lib='',version='', def libc_ver(executable=sys.executable,lib='',version='',
chunksize=2048): chunksize=16384):
""" Tries to determine the libc version that the file executable """ Tries to determine the libc version that the file executable
(which defaults to the Python interpreter) is linked against. (which defaults to the Python interpreter) is linked against.
@ -159,17 +159,22 @@ def libc_ver(executable=sys.executable,lib='',version='',
# able to open symlinks for reading # able to open symlinks for reading
executable = os.path.realpath(executable) executable = os.path.realpath(executable)
f = open(executable,'rb') f = open(executable,'rb')
binary = f.read(chunksize).decode('latin-1') binary = f.read(chunksize)
pos = 0 pos = 0
while 1: while 1:
m = _libc_search.search(binary,pos) if b'libc' in binary or b'GLIBC' in binary:
m = _libc_search.search(binary,pos)
else:
m = None
if not m: if not m:
binary = f.read(chunksize).decode('latin-1') binary = f.read(chunksize)
if not binary: if not binary:
break break
pos = 0 pos = 0
continue continue
libcinit,glibc,glibcversion,so,threads,soversion = m.groups() libcinit,glibc,glibcversion,so,threads,soversion = [
s.decode('latin1') if s is not None else s
for s in m.groups()]
if libcinit and not lib: if libcinit and not lib:
lib = 'libc' lib = 'libc'
elif glibc: elif glibc: