Skip to content

Commit 045aa40

Browse files
committed
Expose the version as a number.
We make lzma_version be the runtime version as that is the one most interestin to users. The compile time version remains exposed as the header version.
1 parent c62ca63 commit 045aa40

File tree

4 files changed

+50
-35
lines changed

4 files changed

+50
-35
lines changed

Doc/library/lzma.rst

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,20 +327,34 @@ the following constants:
327327

328328

329329
.. data:: LZMA_VERSION
330+
.. data:: LZMA_VERSION_STRING
330331

331-
The version string of the lzma library that was used for building the module.
332-
This may be different from the lzma library actually used at runtime, which
333-
is available as :const:`LZMA_RUNTIME_VERSION`.
332+
The version of the lzma C library actually loaded at runtime, in both
333+
integer and string forms.
334334

335335
.. versionadded:: 3.13
336336

337+
.. data:: LZMA_HEADER_VERSION
338+
.. data:: LZMA_HEADER_VERSION_STRING
337339

338-
.. data:: LZMA_RUNTIME_VERSION
339-
340-
The version string of the lzma library actually loaded by the interpreter.
340+
The version of the lzma library that was used for building the module, in
341+
both integer and string forms. This may be different from the lzma library
342+
actually used at runtime.
341343

342344
.. versionadded:: 3.13
343345

346+
The version number and string formats are as defined in by C library. The
347+
integer is represented in decimal digits as ``MJJJPPPS`` where ``M`` is the
348+
major version, ``JJJ`` is the minor version, ``PPP`` is the patch level, and
349+
``S`` is the "stability indicator" (2 means stable)::
350+
351+
>>> import lzma
352+
>>> lzma.LZMA_VERSION
353+
50020052
354+
>>> lzma.LZMA_VERSION_STRING
355+
'5.2.5'
356+
357+
344358
.. _filter-chain-specs:
345359

346360
Specifying custom filter chains
@@ -373,13 +387,13 @@ options. Valid filter IDs are as follows:
373387

374388
* :const:`!FILTER_ARM64`
375389

376-
Only works if :data:`LZMA_RUNTIME_VERSION` is 5.4.0 or later.
390+
Only works if the lzma version is 5.4.0 or later.
377391

378392
.. versionadded:: 3.13
379393

380394
* :const:`!FILTER_RISCV`
381395

382-
Only works if :data:`LZMA_RUNTIME_VERSION` is 5.6.0 or later.
396+
Only works if the lzma version is 5.6.0 or later.
383397

384398
.. versionadded:: 3.13
385399

Lib/lzma.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"FILTER_ARM", "FILTER_ARMTHUMB", "FILTER_POWERPC", "FILTER_SPARC",
1616
"FILTER_ARM64", "FILTER_RISCV",
1717
"FORMAT_AUTO", "FORMAT_XZ", "FORMAT_ALONE", "FORMAT_RAW",
18+
"LZMA_HEADER_VERSION", "LZMA_HEADER_VERSION_STRING",
19+
"LZMA_VERSION", "LZMA_VERSION_STRING",
1820
"MF_HC3", "MF_HC4", "MF_BT2", "MF_BT3", "MF_BT4",
1921
"MODE_FAST", "MODE_NORMAL", "PRESET_DEFAULT", "PRESET_EXTREME",
2022

Lib/test/test_lzma.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@
2020
from lzma import LZMACompressor, LZMADecompressor, LZMAError, LZMAFile
2121

2222

23-
def _lzma_runtime_version_tuple(lzma_version=lzma.LZMA_RUNTIME_VERSION):
24-
v = lzma_version.split('.', 3)
25-
if not v[-1].isnumeric():
26-
v[-1] = re.search(r'\d+', v[-1]).group()
27-
return tuple(map(int, v))
28-
29-
30-
LZMA_RUNTIME_VERSION_TUPLE = _lzma_runtime_version_tuple()
31-
32-
3323
class CompressorDecompressorTestCase(unittest.TestCase):
3424

3525
# Test error cases.
@@ -395,21 +385,20 @@ def test_uninitialized_LZMADecompressor_crash(self):
395385
self.assertEqual(LZMADecompressor.__new__(LZMADecompressor).
396386
decompress(bytes()), b'')
397387

398-
# Test the existence of the relatively new BCJ filters. These just
399-
# ensure that the constant was found at compile time and exposed.
400-
401-
@unittest.skipUnless(
402-
LZMA_RUNTIME_VERSION_TUPLE >= (5, 6, 0),
403-
"RISC-V filter is only available on lzma 5.6.0 or later")
404-
def test_riscv_filter_exists(self):
388+
def test_riscv_filter_constant_exists(self):
405389
self.assertTrue(lzma.FILTER_RISCV)
406390

407-
@unittest.skipUnless(
408-
LZMA_RUNTIME_VERSION_TUPLE >= (5, 4, 0),
409-
"ARM64 filter is only available on lzma 5.4.0 or later")
410-
def test_arm64_filter_exists(self):
391+
def test_arm64_filter_constant_exists(self):
411392
self.assertTrue(lzma.FILTER_ARM64)
412393

394+
def test_lzma_header_versions(self):
395+
self.assertIsInstance(lzma.LZMA_HEADER_VERSION_STRING, str)
396+
self.assertGreater(lzma.LZMA_HEADER_VERSION, 0)
397+
398+
def test_lzma_versions(self):
399+
self.assertIsInstance(lzma.LZMA_VERSION_STRING, str)
400+
self.assertGreater(lzma.LZMA_VERSION, 0)
401+
413402

414403
class CompressDecompressFunctionTestCase(unittest.TestCase):
415404

Modules/_lzmamodule.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
/*
2929
* If the lzma.h we're building against is so old as not to define these, this
3030
* provides their equivalent values so that the names remain defined in Python
31-
* regardless. lzma.LZMA_RUNTIME_VERSION is exposed to Python and is what
32-
* people can use to decide if they can use them at runtime.
31+
* regardless of the header versions used at build time.
3332
*/
3433
#ifndef LZMA_FILTER_ARM64
3534
#define LZMA_FILTER_ARM64 LZMA_VLI_C(0x0A)
@@ -1611,12 +1610,23 @@ lzma_exec(PyObject *module)
16111610
return -1;
16121611
}
16131612

1614-
if (PyModule_Add(module, "LZMA_VERSION",
1615-
PyUnicode_FromString(LZMA_VERSION_STRING)) < 0) {
1613+
if (PyModule_AddStringConstant(
1614+
module, "LZMA_HEADER_VERSION_STRING", LZMA_VERSION_STRING) < 0) {
16161615
return -1;
16171616
}
1618-
if (PyModule_Add(module, "LZMA_RUNTIME_VERSION",
1619-
PyUnicode_FromString(lzma_version_string())) < 0) {
1617+
if (PyModule_AddStringConstant(
1618+
module, "LZMA_VERSION_STRING",
1619+
lzma_version_string()) < 0) {
1620+
return -1;
1621+
}
1622+
PyObject *uint32_obj = PyLong_FromUnsignedLong(LZMA_VERSION);
1623+
if (PyModule_AddObject(module, "LZMA_HEADER_VERSION", uint32_obj) < 0) {
1624+
Py_XDECREF(uint32_obj);
1625+
return -1;
1626+
}
1627+
uint32_obj = PyLong_FromUnsignedLong(lzma_version_number());
1628+
if (PyModule_AddObject(module, "LZMA_VERSION", uint32_obj) < 0) {
1629+
Py_XDECREF(uint32_obj);
16201630
return -1;
16211631
}
16221632

0 commit comments

Comments
 (0)