Skip to content

Commit 5720f7f

Browse files
[3.12] gh-112769: test_zlib: Fix comparison of ZLIB_RUNTIME_VERSION with non-int suffix (GH-112771) (GH-112773)
zlib-ng defines the version as "1.3.0.zlib-ng". (cherry picked from commit d384813) Co-authored-by: Miro Hrončok <miro@hroncok.cz>
1 parent ef92e9e commit 5720f7f

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

Lib/test/test_zlib.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@
1919
hasattr(zlib.decompressobj(), "copy"),
2020
'requires Decompress.copy()')
2121

22+
23+
def _zlib_runtime_version_tuple(zlib_version=zlib.ZLIB_RUNTIME_VERSION):
24+
# Register "1.2.3" as "1.2.3.0"
25+
# or "1.2.0-linux","1.2.0.f","1.2.0.f-linux"
26+
v = zlib_version.split('-', 1)[0].split('.')
27+
if len(v) < 4:
28+
v.append('0')
29+
elif not v[-1].isnumeric():
30+
v[-1] = '0'
31+
return tuple(map(int, v))
32+
33+
34+
ZLIB_RUNTIME_VERSION_TUPLE = _zlib_runtime_version_tuple()
35+
36+
2237
# bpo-46623: On s390x, when a hardware accelerator is used, using different
2338
# ways to compress data with zlib can produce different compressed data.
2439
# Simplified test_pair() code:
@@ -474,9 +489,8 @@ def test_flushes(self):
474489
sync_opt = ['Z_NO_FLUSH', 'Z_SYNC_FLUSH', 'Z_FULL_FLUSH',
475490
'Z_PARTIAL_FLUSH']
476491

477-
ver = tuple(int(v) for v in zlib.ZLIB_RUNTIME_VERSION.split('.'))
478492
# Z_BLOCK has a known failure prior to 1.2.5.3
479-
if ver >= (1, 2, 5, 3):
493+
if ZLIB_RUNTIME_VERSION_TUPLE >= (1, 2, 5, 3):
480494
sync_opt.append('Z_BLOCK')
481495

482496
sync_opt = [getattr(zlib, opt) for opt in sync_opt
@@ -794,16 +808,7 @@ def test_large_unconsumed_tail(self, size):
794808

795809
def test_wbits(self):
796810
# wbits=0 only supported since zlib v1.2.3.5
797-
# Register "1.2.3" as "1.2.3.0"
798-
# or "1.2.0-linux","1.2.0.f","1.2.0.f-linux"
799-
v = zlib.ZLIB_RUNTIME_VERSION.split('-', 1)[0].split('.')
800-
if len(v) < 4:
801-
v.append('0')
802-
elif not v[-1].isnumeric():
803-
v[-1] = '0'
804-
805-
v = tuple(map(int, v))
806-
supports_wbits_0 = v >= (1, 2, 3, 5)
811+
supports_wbits_0 = ZLIB_RUNTIME_VERSION_TUPLE >= (1, 2, 3, 5)
807812

808813
co = zlib.compressobj(level=1, wbits=15)
809814
zlib15 = co.compress(HAMLET_SCENE) + co.flush()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The tests now correctly compare zlib version when
2+
:const:`zlib.ZLIB_RUNTIME_VERSION` contains non-integer suffixes. For
3+
example zlib-ng defines the version as ``1.3.0.zlib-ng``.

0 commit comments

Comments
 (0)