Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-115988: Add missing ARM64 and RISCV filter in lzma module #115989

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 39 additions & 6 deletions Doc/library/lzma.rst
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,25 @@ 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:: 3.13


.. data:: LZMA_RUNTIME_VERSION

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

.. versionadded:: 3.13

.. _filter-chain-specs:

Specifying custom filter chains
Expand All @@ -343,12 +362,26 @@ options. Valid filter IDs are as follows:

* Branch-Call-Jump (BCJ) filters:

* :const:`FILTER_X86`
* :const:`FILTER_IA64`
* :const:`FILTER_ARM`
* :const:`FILTER_ARMTHUMB`
* :const:`FILTER_POWERPC`
* :const:`FILTER_SPARC`
* :const:`!FILTER_X86`
* :const:`!FILTER_IA64`
* :const:`!FILTER_ARM`
* :const:`!FILTER_ARMTHUMB`
* :const:`!FILTER_POWERPC`
* :const:`!FILTER_SPARC`

The above work on all lzma runtime library versions.

* :const:`!FILTER_ARM64`

Only works if :data:`LZMA_RUNTIME_VERSION` is 5.4.0 or later.

.. versionadded:: 3.13

* :const:`!FILTER_RISCV`

Only works if :data:`LZMA_RUNTIME_VERSION` is 5.6.0 or later.

.. versionadded:: 3.13

A filter chain can consist of up to 4 filters, and cannot be empty. The last
filter in the chain must be a compression filter, and any other filters must be
Expand Down
14 changes: 14 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,20 @@ itertools
than the specified batch size.
(Contributed by Raymond Hettinger in :gh:`113202`.)

lzma
----

* Add :const:`lzma.LZMA_VERSION` and :const:`lzma.LZMA_RUNTIME_VERSION`. The former
reports the version string of the underlying ``lzma`` library during build while
the later reports runtime version.
(Contributed by Chien Wong in :gh:`115988`.)

* Add support of new BCJ filters ARM64 and RISC-V via :const:`!lzma.FILTER_ARM64` and
:const:`!lzma.FILTER_RISCV`.
Note that the new filters will work only if runtime library supports them. ARM64 filter
requires ``lzma`` 5.4.0 or newer while RISC-V requires 5.6.0 or newer.
(Contributed by Chien Wong in :gh:`115988`.)

marshal
-------

Expand Down
1 change: 1 addition & 0 deletions Lib/lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"CHECK_ID_MAX", "CHECK_UNKNOWN",
"FILTER_LZMA1", "FILTER_LZMA2", "FILTER_DELTA", "FILTER_X86", "FILTER_IA64",
"FILTER_ARM", "FILTER_ARMTHUMB", "FILTER_POWERPC", "FILTER_SPARC",
"FILTER_ARM64", "FILTER_RISCV",
"FORMAT_AUTO", "FORMAT_XZ", "FORMAT_ALONE", "FORMAT_RAW",
"MF_HC3", "MF_HC4", "MF_BT2", "MF_BT3", "MF_BT4",
"MODE_FAST", "MODE_NORMAL", "PRESET_DEFAULT", "PRESET_EXTREME",
Expand Down
26 changes: 26 additions & 0 deletions Lib/test/test_lzma.py
gpshead marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pathlib
import pickle
import random
import re
import sys
from test import support
import unittest
Expand All @@ -19,6 +20,16 @@
from lzma import LZMACompressor, LZMADecompressor, LZMAError, LZMAFile


def _lzma_runtime_version_tuple(lzma_version=lzma.LZMA_RUNTIME_VERSION):
v = lzma_version.split('.', 3)
if not v[-1].isnumeric():
v[-1] = re.search(r'\d+', v[-1]).group()
return tuple(map(int, v))


LZMA_RUNTIME_VERSION_TUPLE = _lzma_runtime_version_tuple()


class CompressorDecompressorTestCase(unittest.TestCase):

# Test error cases.
Expand Down Expand Up @@ -384,6 +395,21 @@ def test_uninitialized_LZMADecompressor_crash(self):
self.assertEqual(LZMADecompressor.__new__(LZMADecompressor).
decompress(bytes()), b'')

# Test the existence of the relatively new BCJ filters. These just
# ensure that the constant was found at compile time and exposed.

@unittest.skipUnless(
LZMA_RUNTIME_VERSION_TUPLE >= (5, 6, 0),
"RISC-V filter is only available on lzma 5.6.0 or later")
def test_riscv_filter_exists(self):
self.assertTrue(lzma.FILTER_RISCV)

@unittest.skipUnless(
LZMA_RUNTIME_VERSION_TUPLE >= (5, 4, 0),
"ARM64 filter is only available on lzma 5.4.0 or later")
def test_arm64_filter_exists(self):
self.assertTrue(lzma.FILTER_ARM64)


class CompressDecompressFunctionTestCase(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The lzma module adds build and runtime version info of the underlying C library
and supports new BCJ filters: ARM64 and RISC-V.
31 changes: 30 additions & 1 deletion Modules/_lzmamodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@
#error "The maximum block size accepted by liblzma is SIZE_MAX."
#endif


/*
* If the lzma.h we're building against is so old as not to define these, this
* provides their equivalent values so that the names remain defined in Python
* regardless. lzma.LZMA_RUNTIME_VERSION is exposed to Python and is what
* people can use to decide if they can use them at runtime.
*/
#ifndef LZMA_FILTER_ARM64
gpshead marked this conversation as resolved.
Show resolved Hide resolved
#define LZMA_FILTER_ARM64 LZMA_VLI_C(0x0A)
#endif
#ifndef LZMA_FILTER_RISCV
#define LZMA_FILTER_RISCV LZMA_VLI_C(0x0B)
#endif

/* On success, return value >= 0
On failure, return -1 */
static inline Py_ssize_t
Expand Down Expand Up @@ -372,6 +386,8 @@ lzma_filter_converter(_lzma_state *state, PyObject *spec, void *ptr)
case LZMA_FILTER_ARM:
case LZMA_FILTER_ARMTHUMB:
case LZMA_FILTER_SPARC:
case LZMA_FILTER_ARM64:
case LZMA_FILTER_RISCV:
gpshead marked this conversation as resolved.
Show resolved Hide resolved
f->options = parse_filter_spec_bcj(state, spec);
return f->options != NULL;
default:
Expand Down Expand Up @@ -490,7 +506,9 @@ build_filter_spec(const lzma_filter *f)
case LZMA_FILTER_IA64:
case LZMA_FILTER_ARM:
case LZMA_FILTER_ARMTHUMB:
case LZMA_FILTER_SPARC: {
case LZMA_FILTER_SPARC:
case LZMA_FILTER_ARM64:
case LZMA_FILTER_RISCV: {
lzma_options_bcj *options = f->options;
if (options) {
ADD_FIELD(options, start_offset);
Expand Down Expand Up @@ -1551,6 +1569,8 @@ lzma_exec(PyObject *module)
ADD_INT_PREFIX_MACRO(module, FILTER_ARMTHUMB);
ADD_INT_PREFIX_MACRO(module, FILTER_SPARC);
ADD_INT_PREFIX_MACRO(module, FILTER_POWERPC);
ADD_INT_PREFIX_MACRO(module, FILTER_ARM64);
ADD_INT_PREFIX_MACRO(module, FILTER_RISCV);
ADD_INT_PREFIX_MACRO(module, MF_HC3);
ADD_INT_PREFIX_MACRO(module, MF_HC4);
ADD_INT_PREFIX_MACRO(module, MF_BT2);
Expand Down Expand Up @@ -1591,6 +1611,15 @@ lzma_exec(PyObject *module)
return -1;
}

if (PyModule_Add(module, "LZMA_VERSION",
PyUnicode_FromString(LZMA_VERSION_STRING)) < 0) {
return -1;
}
if (PyModule_Add(module, "LZMA_RUNTIME_VERSION",
gpshead marked this conversation as resolved.
Show resolved Hide resolved
PyUnicode_FromString(lzma_version_string())) < 0) {
return -1;
}

return 0;
}

Expand Down