Skip to content

Commit 35960c1

Browse files
committed
Platform tests and bug fixes.
In adding tests for the new platform code I found a bug in the old code: if a valid version is passed for version and it is greater than the version found for an so *and* there is no glibc version, then the version from the argument was returned. The code changes here fix that, as well as fixing my own broken additions.
1 parent 2348711 commit 35960c1

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

Lib/platform.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,26 +190,27 @@ def libc_ver(executable=None, lib='', version='', chunksize=16384):
190190
return lib, version
191191

192192
libc_search = re.compile(
193-
b'(__libc_init)'
194-
b'|'
195-
b'(GLIBC_([0-9.]+))'
196-
b'|'
193+
br'(__libc_init)'
194+
br'|'
195+
br'(GLIBC_([0-9.]+))'
196+
br'|'
197197
br'(libc(_\w+)?\.so(?:\.(\d[0-9.]*))?)'
198-
b'|'
199-
b'(musl-([0-9.]+))'
200-
b'',
198+
br'|'
199+
br'(musl-([0-9.]+))'
200+
br'',
201201
re.ASCII)
202202

203203
V = _comparable_version
204204
# We use os.path.realpath()
205205
# here to work around problems with Cygwin not being
206206
# able to open symlinks for reading
207207
executable = os.path.realpath(executable)
208+
ver = None
208209
with open(executable, 'rb') as f:
209210
binary = f.read(chunksize)
210211
pos = 0
211212
while pos < len(binary):
212-
if b'libc' in binary or b'GLIBC' in binary:
213+
if b'libc' in binary or b'GLIBC' or 'musl' in binary:
213214
m = libc_search.search(binary, pos)
214215
else:
215216
m = None
@@ -229,21 +230,22 @@ def libc_ver(executable=None, lib='', version='', chunksize=16384):
229230
elif glibc:
230231
if lib != 'glibc':
231232
lib = 'glibc'
232-
version = glibcversion
233-
elif V(glibcversion) > V(version):
234-
version = glibcversion
233+
ver = glibcversion
234+
elif V(glibcversion) > V(ver):
235+
ver = glibcversion
235236
elif so:
236237
if lib != 'glibc':
237238
lib = 'libc'
238-
if soversion and (not version or V(soversion) > V(version)):
239-
version = soversion
240-
if threads and version[-len(threads):] != threads:
241-
version = version + threads
239+
if soversion and (not ver or V(soversion) > V(ver)):
240+
ver = soversion
241+
if threads and ver[-len(threads):] != threads:
242+
ver = ver + threads
242243
elif musl:
243244
lib = 'musl'
244-
version = muslversion
245+
if not ver or V(muslversion) > V(ver):
246+
ver = muslversion
245247
pos = m.end()
246-
return lib, version
248+
return lib, version if ver is None else ver
247249

248250
def _norm_version(version, build=''):
249251

Lib/test/test_platform.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ def test_libc_ver(self):
552552
(b'GLIBC_2.9', ('glibc', '2.9')),
553553
(b'libc.so.1.2.5', ('libc', '1.2.5')),
554554
(b'libc_pthread.so.1.2.5', ('libc', '1.2.5_pthread')),
555+
(b'/aports/main/musl/src/musl-1.2.5', ('musl', '1.2.5')),
555556
(b'', ('', '')),
556557
):
557558
with open(filename, 'wb') as fp:
@@ -563,14 +564,29 @@ def test_libc_ver(self):
563564
expected)
564565

565566
# binary containing multiple versions: get the most recent,
566-
# make sure that 1.9 is seen as older than 1.23.4
567-
chunksize = 16384
568-
with open(filename, 'wb') as f:
569-
# test match at chunk boundary
570-
f.write(b'x'*(chunksize - 10))
571-
f.write(b'GLIBC_1.23.4\0GLIBC_1.9\0GLIBC_1.21\0')
572-
self.assertEqual(platform.libc_ver(filename, chunksize=chunksize),
573-
('glibc', '1.23.4'))
567+
# make sure that eg 1.9 is seen as older than 1.23.4, and that
568+
# the arguments don't count even if they are set.
569+
chunksize = 200
570+
for data, expected in (
571+
(b'GLIBC_1.23.4\0GLIBC_1.9\0GLIBC_1.21\0', ('glibc', '1.23.4')),
572+
(b'libc.so.2.4\0libc.so.9\0libc.so.23.1\0', ('libc', '23.1')),
573+
(b'musl-1.4.1\0musl-2.1.1\0musl-2.0.1\0', ('musl', '2.1.1')),
574+
(b'no match here, so defaults are used', ('test', '100.1.0')),
575+
):
576+
with open(filename, 'wb') as f:
577+
# test match at chunk boundary
578+
f.write(b'x'*(chunksize - 10))
579+
f.write(data)
580+
self.assertEqual(
581+
expected,
582+
platform.libc_ver(
583+
filename,
584+
lib='test',
585+
version='100.1.0',
586+
chunksize=chunksize,
587+
),
588+
)
589+
574590

575591
def test_android_ver(self):
576592
res = platform.android_ver()

0 commit comments

Comments
 (0)