gh-117404: Add structured version info for compression modules by serhiy-storchaka · Pull Request #117405 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Doc/library/bz2.rst
42 changes: 42 additions & 0 deletions Doc/library/lzma.rst
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,48 @@ Miscellaneous
feature set.


Information about the version of the lzma library in use is available through
the following constants:


.. data:: LZMA_VERSION

The version string of the lzma library that was used for building the module.
This may be different from the lzma library actually used at runtime, which
is available as :const:`LZMA_RUNTIME_VERSION`.

.. versionadded:: next


.. data:: LZMA_RUNTIME_VERSION

The version string of the lzma library actually loaded by the interpreter.

.. versionadded:: next


.. data:: lzma_version

A named tuple containing the four components of the lzma library
version that was used for building the module:
*major*, *minor*, *patch*, and *stability*.
All values except *stability* are integers; *stability* is ``'alpha'``,
``'beta'``, or ``'stable'``.
The components can also be accessed by name, so ``lzma.lzma_version[0]``
is equivalent to ``lzma.lzma_version.major`` and so on.
This may be different from the lzma library actually used at runtime, which
is available as :const:`lzma_runtime_version`.

.. versionadded:: next


.. data:: lzma_runtime_version

A named tuple containing the lzma library version actually loaded by the interpreter.

.. versionadded:: next


.. _filter-chain-specs:

Specifying custom filter chains
Expand Down
50 changes: 48 additions & 2 deletions Doc/library/zlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -489,17 +489,63 @@ the following constants:
.. versionadded:: 3.3


.. data:: zlib_version

A named tuple containing the four components of the zlib library
version that was used for building the module:
*major*, *minor*, *revision*, and *subversion*.
All values are integers.
The components can also be accessed by name, so ``zlib.zlib_version[0]``
is equivalent to ``zlib.zlib_version.major`` and so on.
This may be different from the zlib library actually used at runtime, which
is available as :const:`zlib_runtime_version`.

.. versionadded:: next


.. data:: zlib_runtime_version

A named tuple containing the zlib library version actually loaded by the interpreter.

.. versionadded:: next


The following constants are only present if zlib-ng was used to build
the module:


.. data:: ZLIBNG_VERSION

The version string of the zlib-ng library that was used for building the
module if zlib-ng was used. When present, the :data:`ZLIB_VERSION` and
:data:`ZLIB_RUNTIME_VERSION` constants reflect the version of the zlib API
provided by zlib-ng.

If zlib-ng was not used to build the module, this constant will be absent.

.. versionadded:: 3.14

.. data:: ZLIBNG_RUNTIME_VERSION

The version string of the zlib-ng library actually loaded
by the interpreter.

.. versionadded:: next


.. data:: zlibng_version

A named tuple containing the version of the zlib-ng library that was
used for building the module if zlib-ng was used.

.. versionadded:: next


.. data:: zlibng_runtime_version

A named tuple containing the zlib-ng library version actually loaded
by the interpreter.

.. versionadded:: next


.. seealso::

Expand Down
4 changes: 2 additions & 2 deletions Lib/bz2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

__all__ = ["BZ2File", "BZ2Compressor", "BZ2Decompressor",
"open", "compress", "decompress"]
"open", "compress", "decompress", "BZLIB_VERSION", "bzlib_version"]

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

Expand All @@ -14,7 +14,7 @@
import io
import os

from _bz2 import BZ2Compressor, BZ2Decompressor
from _bz2 import BZ2Compressor, BZ2Decompressor, BZLIB_VERSION, bzlib_version


# Value 0 no longer used
Expand Down
1 change: 1 addition & 0 deletions Lib/lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

"LZMACompressor", "LZMADecompressor", "LZMAFile", "LZMAError",
"open", "compress", "decompress", "is_check_supported",
"LZMA_VERSION", "lzma_version", "LZMA_RUNTIME_VERSION", "lzma_runtime_version",
]

import builtins
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_bz2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,31 @@ def test_newline(self):
self.assertEqual(f.readlines(), [text])


class MiscTests(unittest.TestCase):

def test_bzlib_version(self):
if support.verbose:
print(f'BZLIB_VERSION = {bz2.BZLIB_VERSION}', flush=True)
print(f'bzlib_version = {bz2.bzlib_version}', flush=True)
v = bz2.bzlib_version
self.assertIsInstance(v[:], tuple)
self.assertEqual(len(v), 3)
self.assertIsInstance(v[0], int)
self.assertIsInstance(v[1], int)
self.assertIsInstance(v[2], int)
self.assertIsInstance(v.major, int)
self.assertIsInstance(v.minor, int)
self.assertIsInstance(v.patch, int)
self.assertEqual(v[0], v.major)
self.assertEqual(v[1], v.minor)
self.assertEqual(v[2], v.patch)
self.assertGreaterEqual(v.major, 0)
self.assertGreaterEqual(v.minor, 0)
self.assertGreaterEqual(v.patch, 0)

self.assertEqual(bz2.BZLIB_VERSION.split(',')[0], '%d.%d.%d' % v)


def tearDownModule():
support.reap_children()

Expand Down
37 changes: 37 additions & 0 deletions Lib/test/test_lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,43 @@ def test_x_mode(self):

class MiscellaneousTestCase(unittest.TestCase):

def _test_lzma_version(self, v, string):
self.assertIsInstance(v[:], tuple)
self.assertEqual(len(v), 4)
self.assertIsInstance(v[0], int)
self.assertIsInstance(v[1], int)
self.assertIsInstance(v[2], int)
self.assertIsInstance(v[3], str)
self.assertIsInstance(v.major, int)
self.assertIsInstance(v.minor, int)
self.assertIsInstance(v.patch, int)
self.assertIsInstance(v.stability, str)
self.assertEqual(v[0], v.major)
self.assertEqual(v[1], v.minor)
self.assertEqual(v[2], v.patch)
self.assertEqual(v[3], v.stability)
self.assertGreaterEqual(v.major, 0)
self.assertGreaterEqual(v.minor, 0)
self.assertGreaterEqual(v.patch, 0)
self.assertIn(v.stability, {'alpha', 'beta', 'stable'})

if v.stability == 'stable':
self.assertEqual(string, '%d.%d.%d' % v[:3])
else:
self.assertTrue(string.startswith('%d.%d.%d%s' % v))

def test_lzma_version(self):
if support.verbose:
print(f'LZMA_VERSION = {lzma.LZMA_VERSION}', flush=True)
print(f'lzma_version = {lzma.lzma_version}', flush=True)
self._test_lzma_version(lzma.lzma_version, lzma.LZMA_VERSION)

def test_lzma_runtime_version(self):
if support.verbose:
print(f'LZMA_RUNTIME_VERSION = {lzma.LZMA_RUNTIME_VERSION}', flush=True)
print(f'lzma_runtime_version = {lzma.lzma_runtime_version}', flush=True)
self._test_lzma_version(lzma.lzma_runtime_version, lzma.LZMA_RUNTIME_VERSION)

def test_is_check_supported(self):
# CHECK_NONE and CHECK_CRC32 should always be supported,
# regardless of the options liblzma was compiled with.
Expand Down
72 changes: 62 additions & 10 deletions Lib/test/test_zlib.py
Loading
Loading