Skip to content

Commit f57fc45

Browse files
gh-117404: Add structured version info for compression modules
Add a number of constants in modules zlib, bz2, lzma, and compression.zstd, which provide information about the version of libraries that were used for building the module and that are actually loaded: * zlib.zlib_version -- an alias of zlib.ZLIB_RUNTIME_VERSION * zlib.ZLIB_VERSION_INFO * zlib.zlib_version_info * zlib.zlibng_version * zlib.ZLIBNG_VERSION_INFO * zlib.zlibng_version_info * bz2.bzlib_version * bz2.bzlib_version_info * lzma.LZMA_VERSION * lzma.lzma_version * lzma.LZMA_VERSION_INFO * lzma.lzma_version_info * compression.zstd.ZSTD_VERSION * compression.zstd.ZSTD_VERSION_INFO Make compression.zstd.zstd_version_info a named tuple.
1 parent 1d4e965 commit f57fc45

18 files changed

Lines changed: 762 additions & 36 deletions

File tree

Doc/library/bz2.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,31 @@ One-shot (de)compression
307307
.. versionchanged:: 3.3
308308
Support for multi-stream inputs was added.
309309

310+
311+
Miscellaneous
312+
-------------
313+
314+
Information about the version of the bzip2 compression library in use
315+
is available through the following constants:
316+
317+
318+
.. data:: bzlib_version
319+
320+
The version string of the bzip2 compression library.
321+
322+
.. versionadded:: next
323+
324+
325+
.. data:: bzlib_version_info
326+
327+
A named tuple containing the three components of the bzip2 compression
328+
library version: *major*, *minor*, and *patch*. All values are integers.
329+
The components can also be accessed by name, so ``bz2.bzlib_version_info[0]``
330+
is equivalent to ``bz2.bzlib_version_info.major`` and so on.
331+
332+
.. versionadded:: next
333+
334+
310335
.. _bz2-usage-examples:
311336

312337
Examples of usage

Doc/library/compression.zstd.rst

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,10 +827,43 @@ Miscellaneous
827827
The default compression level for Zstandard: ``3``.
828828

829829

830-
.. attribute:: zstd_version_info
830+
Information about the version of the zstd library in use is available through
831+
the following constants:
831832

832-
Version number of the runtime zstd library as a tuple of integers
833-
(major, minor, release).
833+
834+
.. data:: ZSTD_VERSION
835+
836+
The version string of the zstd library that was used for building the module.
837+
This may be different from the zstd library actually used at runtime, which
838+
is available as :const:`zstd_version`.
839+
840+
.. versionadded:: next
841+
842+
843+
.. data:: zstd_version
844+
845+
The version string of the zstd library actually loaded by the interpreter.
846+
847+
848+
.. data:: ZSTD_VERSION_INFO
849+
850+
A named tuple containing the four components of the zstd library
851+
version that was used for building the module:
852+
*major*, *minor*, and *patch*. All values are integers.
853+
The components can also be accessed by name, so ``zstd.ZSTD_VERSION_INFO[0]``
854+
is equivalent to ``zstd.ZSTD_VERSION_INFO.major`` and so on.
855+
This may be different from the zstd library actually used at runtime, which
856+
is available as :const:`zstd_version_info`.
857+
858+
.. versionadded:: next
859+
860+
861+
.. data:: zstd_version_info
862+
863+
A named tuple containing the zstd library version actually loaded by the interpreter.
864+
865+
.. versionchanged:: next
866+
It is now a named tuple.
834867

835868

836869
Examples

Doc/library/lzma.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,48 @@ Miscellaneous
335335
feature set.
336336

337337

338+
Information about the version of the lzma library in use is available through
339+
the following constants:
340+
341+
342+
.. data:: LZMA_VERSION
343+
344+
The version string of the lzma library that was used for building the module.
345+
This may be different from the lzma library actually used at runtime, which
346+
is available as :const:`lzma_version`.
347+
348+
.. versionadded:: next
349+
350+
351+
.. data:: lzma_version
352+
353+
The version string of the lzma library actually loaded by the interpreter.
354+
355+
.. versionadded:: next
356+
357+
358+
.. data:: LZMA_VERSION_INFO
359+
360+
A named tuple containing the four components of the lzma library
361+
version that was used for building the module:
362+
*major*, *minor*, *patch*, and *stability*.
363+
All values except *stability* are integers; *stability* is ``'alpha'``,
364+
``'beta'``, or ``'stable'``.
365+
The components can also be accessed by name, so ``lzma.LZMA_VERSION_INFO[0]``
366+
is equivalent to ``lzma.LZMA_VERSION_INFO.major`` and so on.
367+
This may be different from the lzma library actually used at runtime, which
368+
is available as :const:`lzma_version_info`.
369+
370+
.. versionadded:: next
371+
372+
373+
.. data:: lzma_version_info
374+
375+
A named tuple containing the lzma library version actually loaded by the interpreter.
376+
377+
.. versionadded:: next
378+
379+
338380
.. _filter-chain-specs:
339381

340382
Specifying custom filter chains

Doc/library/zlib.rst

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,27 +479,76 @@ the following constants:
479479

480480
The version string of the zlib library that was used for building the module.
481481
This may be different from the zlib library actually used at runtime, which
482-
is available as :const:`ZLIB_RUNTIME_VERSION`.
482+
is available as :const:`zlib_version`.
483483

484484

485485
.. data:: ZLIB_RUNTIME_VERSION
486+
.. data:: zlib_version
486487

487488
The version string of the zlib library actually loaded by the interpreter.
488489

489490
.. versionadded:: 3.3
491+
.. versionchanged:: next
492+
Added alias :const:`!zlib_version`.
493+
494+
495+
.. data:: ZLIB_VERSION_INFO
496+
497+
A named tuple containing the four components of the zlib library
498+
version that was used for building the module:
499+
*major*, *minor*, *revision*, and *subversion*.
500+
All values are integers.
501+
The components can also be accessed by name, so ``zlib.VERSION_INFO[0]``
502+
is equivalent to ``zlib.VERSION_INFO.major`` and so on.
503+
This may be different from the zlib library actually used at runtime, which
504+
is available as :const:`zlib_version_info`.
505+
506+
.. versionadded:: next
507+
508+
509+
.. data:: zlib_version_info
510+
511+
A named tuple containing the zlib library version actually loaded by the interpreter.
512+
513+
.. versionadded:: next
514+
515+
516+
The following constants are only present if zlib-ng was used to build
517+
the module:
490518

491519

492520
.. data:: ZLIBNG_VERSION
493521

494522
The version string of the zlib-ng library that was used for building the
495-
module if zlib-ng was used. When present, the :data:`ZLIB_VERSION` and
496-
:data:`ZLIB_RUNTIME_VERSION` constants reflect the version of the zlib API
523+
module if zlib-ng was used. When present, the :const:`ZLIB_VERSION` and
524+
:const:`zlib_version` constants reflect the version of the zlib API
497525
provided by zlib-ng.
498526

499-
If zlib-ng was not used to build the module, this constant will be absent.
500-
501527
.. versionadded:: 3.14
502528

529+
.. data:: zlibng_version
530+
531+
The version string of the zlib-ng library actually loaded
532+
by the interpreter.
533+
534+
.. versionadded:: next
535+
536+
537+
.. data:: ZLIBNG_VERSION_INFO
538+
539+
A named tuple containing the version of the zlib-ng library that was
540+
used for building the module if zlib-ng was used.
541+
542+
.. versionadded:: next
543+
544+
545+
.. data:: zlibng_version_info
546+
547+
A named tuple containing the zlib-ng library version actually loaded
548+
by the interpreter.
549+
550+
.. versionadded:: next
551+
503552

504553
.. seealso::
505554

Doc/whatsnew/3.16.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ New modules
8686
Improved modules
8787
================
8888

89+
bz2
90+
---
91+
92+
* Added constants :const:`~bz2.bzlib_version` and
93+
:const:`~bz2.bzlib_version_info` which provide information about
94+
the version of the bzip2 compression library in use.
95+
(Contributed by Serhiy Storchaka in :gh:`117404`.)
96+
97+
compression.zstd
98+
----------------
99+
100+
* Added constants :const:`~compression.zstd.ZSTD_VERSION` and
101+
:const:`~compression.zstd.ZSTD_VERSION_INFO` which provide information
102+
about the version of the zstd library that was used for building the module.
103+
:const:`~compression.zstd.zstd_version_info` is now a named tuple.
104+
(Contributed by Serhiy Storchaka in :gh:`117404`.)
105+
89106
gzip
90107
----
91108

@@ -102,6 +119,11 @@ lzma
102119
requires ``lzma`` 5.4.0 or newer while RISC-V requires 5.6.0 or newer.
103120
(Contributed by Chien Wong in :gh:`115988`.)
104121

122+
* Added constants :const:`~lzma.LZMA_VERSION`, :const:`~lzma.lzma_version`,
123+
:const:`~lzma.LZMA_VERSION_INFO`, and :const:`~lzma.lzma_version_info`,
124+
which provide information about the version of the lzma library in use.
125+
(Contributed by Serhiy Storchaka in :gh:`117404`.)
126+
105127
os
106128
--
107129

@@ -124,6 +146,18 @@ xml
124146
instead of failing later, when encounter non-ASCII data.
125147
(Contributed by Serhiy Storchaka in :gh:`62259`.)
126148

149+
zlib
150+
----
151+
152+
* Added constants :const:`~zlib.ZLIB_VERSION_INFO`,
153+
:const:`~zlib.zlib_version_info`, :const:`~zlib.zlibng_version`,
154+
:const:`~zlib.ZLIBNG_VERSION_INFO`, and :const:`~zlib.zlibng_version_info`,
155+
which provide information about the version of the zlib and the zlib-ng
156+
libraries in use.
157+
Added :const:`~zlib.zlib_version` as an alias of
158+
:const:`~zlib.ZLIB_RUNTIME_VERSION`.
159+
(Contributed by Serhiy Storchaka in :gh:`117404`.)
160+
127161
.. Add improved modules above alphabetically, not here at the end.
128162
129163
Optimizations

Lib/bz2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
__all__ = ["BZ2File", "BZ2Compressor", "BZ2Decompressor",
8-
"open", "compress", "decompress"]
8+
"open", "compress", "decompress", "bzlib_version", "bzlib_version_info"]
99

1010
__author__ = "Nadeem Vawda <nadeem.vawda@gmail.com>"
1111

@@ -14,7 +14,7 @@
1414
import io
1515
import os
1616

17-
from _bz2 import BZ2Compressor, BZ2Decompressor
17+
from _bz2 import BZ2Compressor, BZ2Decompressor, bzlib_version, bzlib_version_info
1818

1919

2020
# Value 0 no longer used

Lib/compression/zstd/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
'get_frame_size',
2121
'zstd_version',
2222
'zstd_version_info',
23+
'ZSTD_VERSION',
24+
'ZSTD_VERSION_INFO',
2325
'ZstdCompressor',
2426
'ZstdDecompressor',
2527
'ZstdDict',
@@ -29,13 +31,10 @@
2931
import _zstd
3032
import enum
3133
from _zstd import (ZstdCompressor, ZstdDecompressor, ZstdDict, ZstdError,
32-
get_frame_size, zstd_version)
34+
get_frame_size, zstd_version, ZSTD_VERSION,
35+
zstd_version_info, ZSTD_VERSION_INFO)
3336
from compression.zstd._zstdfile import ZstdFile, open, _nbytes
3437

35-
# zstd_version_number is (MAJOR * 100 * 100 + MINOR * 100 + RELEASE)
36-
zstd_version_info = (*divmod(_zstd.zstd_version_number // 100, 100),
37-
_zstd.zstd_version_number % 100)
38-
"""Version number of the runtime zstd library as a tuple of integers."""
3938

4039
COMPRESSION_LEVEL_DEFAULT = _zstd.ZSTD_CLEVEL_DEFAULT
4140
"""The default compression level for Zstandard, currently '3'."""

Lib/lzma.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
"LZMACompressor", "LZMADecompressor", "LZMAFile", "LZMAError",
2222
"open", "compress", "decompress", "is_check_supported",
23+
"LZMA_VERSION", "lzma_version", "LZMA_RUNTIME_VERSION", "lzma_runtime_version",
2324
]
2425

2526
import builtins

Lib/test/pythoninfo.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,17 +660,37 @@ def collect_zlib(info_add):
660660
except ImportError:
661661
return
662662

663-
attributes = ('ZLIB_VERSION', 'ZLIB_RUNTIME_VERSION', 'ZLIBNG_VERSION')
663+
attributes = ('ZLIB_VERSION', 'zlib_version', 'ZLIBNG_VERSION', 'zlibng_version')
664664
copy_attributes(info_add, zlib, 'zlib.%s', attributes)
665665

666666

667+
def collect_bz2(info_add):
668+
try:
669+
import _bz2
670+
except ImportError:
671+
return
672+
673+
attributes = ('bzlib_version',)
674+
copy_attributes(info_add, _bz2, 'bz2.%s', attributes)
675+
676+
677+
def collect_lzma(info_add):
678+
try:
679+
import _lzma
680+
except ImportError:
681+
return
682+
683+
attributes = ('LZMA_VERSION', 'lzma_version')
684+
copy_attributes(info_add, _lzma, 'lzma.%s', attributes)
685+
686+
667687
def collect_zstd(info_add):
668688
try:
669689
import _zstd
670690
except ImportError:
671691
return
672692

673-
attributes = ('zstd_version',)
693+
attributes = ('ZSTD_VERSION', 'zstd_version')
674694
copy_attributes(info_add, _zstd, 'zstd.%s', attributes)
675695

676696

Lib/test/test_bz2.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,31 @@ def test_newline(self):
12071207
self.assertEqual(f.readlines(), [text])
12081208

12091209

1210+
class MiscTests(unittest.TestCase):
1211+
1212+
def test_bzlib_version(self):
1213+
if support.verbose:
1214+
print(f'bzlib_version = {bz2.bzlib_version}', flush=True)
1215+
print(f'bzlib_version_info = {bz2.bzlib_version_info}', flush=True)
1216+
v = bz2.bzlib_version_info
1217+
self.assertIsInstance(v[:], tuple)
1218+
self.assertEqual(len(v), 3)
1219+
self.assertIsInstance(v[0], int)
1220+
self.assertIsInstance(v[1], int)
1221+
self.assertIsInstance(v[2], int)
1222+
self.assertIsInstance(v.major, int)
1223+
self.assertIsInstance(v.minor, int)
1224+
self.assertIsInstance(v.patch, int)
1225+
self.assertEqual(v[0], v.major)
1226+
self.assertEqual(v[1], v.minor)
1227+
self.assertEqual(v[2], v.patch)
1228+
self.assertGreaterEqual(v.major, 0)
1229+
self.assertGreaterEqual(v.minor, 0)
1230+
self.assertGreaterEqual(v.patch, 0)
1231+
1232+
self.assertEqual(bz2.bzlib_version.split(',')[0], '%d.%d.%d' % v)
1233+
1234+
12101235
def tearDownModule():
12111236
support.reap_children()
12121237

0 commit comments

Comments
 (0)