Skip to content

Commit a844747

Browse files
gh-117404: Add structured version info for compression modules (GH-150567)
Add constants which provide information about the version of the compression library used for building the module and the version actually loaded: * zlib: ZLIB_VERSION_INFO, zlib_version_info, ZLIBNG_VERSION_INFO, and zlib_version as an alias of ZLIB_RUNTIME_VERSION * bz2: bzlib_version, bzlib_version_info * lzma: LZMA_VERSION, lzma_version, LZMA_VERSION_INFO, lzma_version_info * compression.zstd: ZSTD_VERSION, ZSTD_VERSION_INFO Make compression.zstd.zstd_version_info a named tuple. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent fd0970c commit a844747

18 files changed

Lines changed: 718 additions & 36 deletions

File tree

Doc/library/bz2.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,36 @@ 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
315+
actually loaded by the interpreter is available through the following
316+
constants.
317+
The version used for building the module is not available,
318+
because the bzip2 library does not provide it.
319+
320+
321+
.. data:: bzlib_version
322+
323+
The version string of the bzip2 compression library
324+
actually loaded by the interpreter.
325+
326+
.. versionadded:: next
327+
328+
329+
.. data:: bzlib_version_info
330+
331+
A named tuple containing the three components of the bzip2 compression
332+
library version actually loaded by the interpreter:
333+
*major*, *minor*, and *patch*. All values are integers.
334+
The components can also be accessed by name, so ``bz2.bzlib_version_info[0]``
335+
is equivalent to ``bz2.bzlib_version_info.major`` and so on.
336+
337+
.. versionadded:: next
338+
339+
310340
.. _bz2-usage-examples:
311341

312342
Examples of usage

Doc/library/compression.zstd.rst

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -827,10 +827,45 @@ 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 three 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 version of the zstd library
864+
actually loaded by the interpreter,
865+
with the same fields as :const:`ZSTD_VERSION_INFO`.
866+
867+
.. versionchanged:: next
868+
It is now a named tuple.
834869

835870

836871
Examples

Doc/library/lzma.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,50 @@ Miscellaneous
314314
feature set.
315315

316316

317+
Information about the version of the lzma library in use is available through
318+
the following constants:
319+
320+
321+
.. data:: LZMA_VERSION
322+
323+
The version string of the lzma library that was used for building the module.
324+
This may be different from the lzma library actually used at runtime, which
325+
is available as :const:`lzma_version`.
326+
327+
.. versionadded:: next
328+
329+
330+
.. data:: lzma_version
331+
332+
The version string of the lzma library actually loaded by the interpreter.
333+
334+
.. versionadded:: next
335+
336+
337+
.. data:: LZMA_VERSION_INFO
338+
339+
A named tuple containing the four components of the lzma library
340+
version that was used for building the module:
341+
*major*, *minor*, *patch*, and *stability*.
342+
All values except *stability* are integers; *stability* is ``'alpha'``,
343+
``'beta'``, or ``'stable'``.
344+
The components can also be accessed by name, so ``lzma.LZMA_VERSION_INFO[0]``
345+
is equivalent to ``lzma.LZMA_VERSION_INFO.major`` and so on.
346+
This may be different from the lzma library actually used at runtime, which
347+
is available as :const:`lzma_version_info`.
348+
349+
.. versionadded:: next
350+
351+
352+
.. data:: lzma_version_info
353+
354+
A named tuple containing the version of the lzma library
355+
actually loaded by the interpreter,
356+
with the same fields as :const:`LZMA_VERSION_INFO`.
357+
358+
.. versionadded:: next
359+
360+
317361
.. _filter-chain-specs:
318362

319363
Specifying custom filter chains

Doc/library/zlib.rst

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,27 +479,65 @@ 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+
zlib_version
486487

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

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

491523

492524
.. data:: ZLIBNG_VERSION
493525

494526
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
527+
module if zlib-ng was used. When present, the :const:`ZLIB_VERSION` and
528+
:const:`zlib_version` constants reflect the version of the zlib API
497529
provided by zlib-ng.
498530

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

533+
.. data:: ZLIBNG_VERSION_INFO
534+
535+
A named tuple containing the three components of the zlib-ng library
536+
version that was used for building the module:
537+
*major*, *minor*, and *revision*. All values are integers.
538+
539+
.. versionadded:: next
540+
503541

504542
.. seealso::
505543

Doc/whatsnew/3.16.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ asyncio
116116
socket file created for *path*.
117117
(Contributed by Sam Bull in :gh:`94984`.)
118118

119+
bz2
120+
---
121+
122+
* Added constants :const:`~bz2.bzlib_version` and
123+
:const:`~bz2.bzlib_version_info` which provide information about
124+
the version of the bzip2 compression library in use.
125+
(Contributed by Serhiy Storchaka in :gh:`117404`.)
119126

120127
codecs
121128
------
@@ -127,6 +134,14 @@ codecs
127134
even when a built-in codec of the same name exists.
128135
(Contributed by Serhiy Storchaka in :gh:`152997`.)
129136

137+
compression.zstd
138+
----------------
139+
140+
* Added constants :const:`~compression.zstd.ZSTD_VERSION` and
141+
:const:`~compression.zstd.ZSTD_VERSION_INFO` which provide information
142+
about the version of the zstd library that was used for building the module.
143+
:const:`~compression.zstd.zstd_version_info` is now a named tuple.
144+
(Contributed by Serhiy Storchaka in :gh:`117404`.)
130145

131146
concurrent.futures
132147
------------------
@@ -439,6 +454,11 @@ lzma
439454
requires ``lzma`` 5.4.0 or newer while RISC-V requires 5.6.0 or newer.
440455
(Contributed by Chien Wong in :gh:`115988`.)
441456

457+
* Added constants :const:`~lzma.LZMA_VERSION`, :const:`~lzma.lzma_version`,
458+
:const:`~lzma.LZMA_VERSION_INFO`, and :const:`~lzma.lzma_version_info`,
459+
which provide information about the version of the lzma library in use.
460+
(Contributed by Serhiy Storchaka in :gh:`117404`.)
461+
442462

443463
math
444464
----
@@ -747,6 +767,17 @@ zipfile
747767
by the local file entries of removed members.
748768
(Contributed by Danny Lin in :gh:`51067`.)
749769

770+
zlib
771+
----
772+
773+
* Added constants :const:`~zlib.ZLIB_VERSION_INFO`,
774+
:const:`~zlib.zlib_version_info`, and :const:`~zlib.ZLIBNG_VERSION_INFO`,
775+
which provide information about the version of the zlib and the zlib-ng
776+
libraries in use.
777+
Added :const:`~zlib.zlib_version` as an alias of
778+
:const:`~zlib.ZLIB_RUNTIME_VERSION`.
779+
(Contributed by Serhiy Storchaka in :gh:`117404`.)
780+
750781
.. Add improved modules above alphabetically, not here at the end.
751782
752783
Optimizations

Lib/bz2.py

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

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

1011
__author__ = "Nadeem Vawda <nadeem.vawda@gmail.com>"
1112

@@ -14,7 +15,8 @@
1415
import io
1516
import os
1617

17-
from _bz2 import BZ2Compressor, BZ2Decompressor
18+
from _bz2 import (BZ2Compressor, BZ2Decompressor,
19+
bzlib_version, bzlib_version_info)
1820

1921

2022
# 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_info", "LZMA_VERSION", "LZMA_VERSION_INFO",
2324
]
2425

2526
import builtins

Lib/test/pythoninfo.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,17 +711,37 @@ def collect_zlib(info_add):
711711
except ImportError:
712712
return
713713

714-
attributes = ('ZLIB_VERSION', 'ZLIB_RUNTIME_VERSION', 'ZLIBNG_VERSION')
714+
attributes = ('ZLIB_VERSION', 'zlib_version', 'ZLIBNG_VERSION')
715715
copy_attributes(info_add, zlib, 'zlib.%s', attributes)
716716

717717

718+
def collect_bz2(info_add):
719+
try:
720+
import _bz2
721+
except ImportError:
722+
return
723+
724+
attributes = ('bzlib_version',)
725+
copy_attributes(info_add, _bz2, 'bz2.%s', attributes)
726+
727+
728+
def collect_lzma(info_add):
729+
try:
730+
import _lzma
731+
except ImportError:
732+
return
733+
734+
attributes = ('LZMA_VERSION', 'lzma_version')
735+
copy_attributes(info_add, _lzma, 'lzma.%s', attributes)
736+
737+
718738
def collect_zstd(info_add):
719739
try:
720740
import _zstd
721741
except ImportError:
722742
return
723743

724-
attributes = ('zstd_version',)
744+
attributes = ('ZSTD_VERSION', 'zstd_version')
725745
copy_attributes(info_add, _zstd, 'zstd.%s', attributes)
726746

727747

@@ -1328,6 +1348,7 @@ def collect_info(info):
13281348
collect_urandom,
13291349

13301350
collect_builtins,
1351+
collect_bz2,
13311352
collect_cc,
13321353
collect_curses,
13331354
collect_datetime,
@@ -1338,6 +1359,7 @@ def collect_info(info):
13381359
collect_gdbm,
13391360
collect_get_config,
13401361
collect_locale,
1362+
collect_lzma,
13411363
collect_os,
13421364
collect_platform,
13431365
collect_pwd,

0 commit comments

Comments
 (0)