Skip to content

Commit ba7c226

Browse files
committed
Make platform.libc_ver() less slow
1 parent 978b9d2 commit ba7c226

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

‎Lib/platform.py‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@
130130

131131
### Platform specific APIs
132132

133-
_libc_search = re.compile(r'(__libc_init)'
134-
'|'
135-
'(GLIBC_([0-9.]+))'
136-
'|'
137-
'(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)', re.ASCII)
133+
_libc_search = re.compile(b'(__libc_init)'
134+
b'|'
135+
b'(GLIBC_([0-9.]+))'
136+
b'|'
137+
br'(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)', re.ASCII)
138138

139139
def libc_ver(executable=sys.executable,lib='',version='',
140140

141-
chunksize=2048):
141+
chunksize=16384):
142142

143143
""" Tries to determine the libc version that the file executable
144144
(which defaults to the Python interpreter) is linked against.
@@ -159,17 +159,22 @@ def libc_ver(executable=sys.executable,lib='',version='',
159159
# able to open symlinks for reading
160160
executable = os.path.realpath(executable)
161161
f = open(executable,'rb')
162-
binary = f.read(chunksize).decode('latin-1')
162+
binary = f.read(chunksize)
163163
pos = 0
164164
while 1:
165-
m = _libc_search.search(binary,pos)
165+
if b'libc' in binary or b'GLIBC' in binary:
166+
m = _libc_search.search(binary,pos)
167+
else:
168+
m = None
166169
if not m:
167-
binary = f.read(chunksize).decode('latin-1')
170+
binary = f.read(chunksize)
168171
if not binary:
169172
break
170173
pos = 0
171174
continue
172-
libcinit,glibc,glibcversion,so,threads,soversion = m.groups()
175+
libcinit,glibc,glibcversion,so,threads,soversion = [
176+
s.decode('latin1') if s is not None else s
177+
for s in m.groups()]
173178
if libcinit and not lib:
174179
lib = 'libc'
175180
elif glibc:

0 commit comments

Comments
 (0)