Skip to content

Commit cf2cd0b

Browse files
ivqgpshead
andauthored
gh-115988: Add ARM64 and RISCV BCJ filters constants in lzma module (GH-115989)
--------- Signed-off-by: Chien Wong <m@xv97.com> Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent f386f1f commit cf2cd0b

6 files changed

Lines changed: 57 additions & 7 deletions

File tree

Doc/library/lzma.rst

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,26 @@ options. Valid filter IDs are as follows:
356356

357357
* Branch-Call-Jump (BCJ) filters:
358358

359-
* :const:`FILTER_X86`
360-
* :const:`FILTER_IA64`
361-
* :const:`FILTER_ARM`
362-
* :const:`FILTER_ARMTHUMB`
363-
* :const:`FILTER_POWERPC`
364-
* :const:`FILTER_SPARC`
359+
* :const:`!FILTER_X86`
360+
* :const:`!FILTER_IA64`
361+
* :const:`!FILTER_ARM`
362+
* :const:`!FILTER_ARMTHUMB`
363+
* :const:`!FILTER_POWERPC`
364+
* :const:`!FILTER_SPARC`
365+
366+
The above work on all lzma runtime library versions.
367+
368+
* :const:`!FILTER_ARM64`
369+
370+
Only works if the lzma version is 5.4.0 or later.
371+
372+
.. versionadded:: next
373+
374+
* :const:`!FILTER_RISCV`
375+
376+
Only works if the lzma version is 5.6.0 or later.
377+
378+
.. versionadded:: next
365379

366380
A filter chain can consist of up to 4 filters, and cannot be empty. The last
367381
filter in the chain must be a compression filter, and any other filters must be

Doc/whatsnew/3.16.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ gzip
9393
which is passed on to the constructor of the :class:`~gzip.GzipFile` class.
9494
(Contributed by Marin Misur in :gh:`91372`.)
9595

96+
lzma
97+
----
98+
99+
* Add support of new BCJ filters ARM64 and RISC-V via
100+
:const:`!lzma.FILTER_ARM64` and :const:`!lzma.FILTER_RISCV`. Note that the
101+
new filters will work only if runtime library supports them. ARM64 filter
102+
requires ``lzma`` 5.4.0 or newer while RISC-V requires 5.6.0 or newer.
103+
(Contributed by Chien Wong in :gh:`115988`.)
104+
96105
os
97106
--
98107

Lib/lzma.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"CHECK_ID_MAX", "CHECK_UNKNOWN",
1414
"FILTER_LZMA1", "FILTER_LZMA2", "FILTER_DELTA", "FILTER_X86", "FILTER_IA64",
1515
"FILTER_ARM", "FILTER_ARMTHUMB", "FILTER_POWERPC", "FILTER_SPARC",
16+
"FILTER_ARM64", "FILTER_RISCV",
1617
"FORMAT_AUTO", "FORMAT_XZ", "FORMAT_ALONE", "FORMAT_RAW",
1718
"MF_HC3", "MF_HC4", "MF_BT2", "MF_BT3", "MF_BT4",
1819
"MODE_FAST", "MODE_NORMAL", "PRESET_DEFAULT", "PRESET_EXTREME",

Lib/test/test_lzma.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ def test_uninitialized_LZMADecompressor_crash(self):
383383
self.assertEqual(LZMADecompressor.__new__(LZMADecompressor).
384384
decompress(bytes()), b'')
385385

386+
def test_riscv_filter_constant_exists(self):
387+
self.assertTrue(lzma.FILTER_RISCV)
388+
389+
def test_arm64_filter_constant_exists(self):
390+
self.assertTrue(lzma.FILTER_ARM64)
391+
386392

387393
class CompressDecompressFunctionTestCase(unittest.TestCase):
388394

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:mod:`lzma` adds constants to support the newer BCJ filters for ARM64 and RISC-V.

Modules/_lzmamodule.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@
2626
#error "The maximum block size accepted by liblzma is SIZE_MAX."
2727
#endif
2828

29+
30+
/*
31+
* If the lzma.h we're building against is so old as not to define these, this
32+
* provides their equivalent values so that the names remain defined in Python
33+
* regardless of the header versions used at build time.
34+
*/
35+
#ifndef LZMA_FILTER_ARM64
36+
#define LZMA_FILTER_ARM64 LZMA_VLI_C(0x0A)
37+
#endif
38+
#ifndef LZMA_FILTER_RISCV
39+
#define LZMA_FILTER_RISCV LZMA_VLI_C(0x0B)
40+
#endif
41+
2942
/* On success, return value >= 0
3043
On failure, return -1 */
3144
static inline Py_ssize_t
@@ -373,6 +386,8 @@ lzma_filter_converter(_lzma_state *state, PyObject *spec, void *ptr)
373386
case LZMA_FILTER_ARM:
374387
case LZMA_FILTER_ARMTHUMB:
375388
case LZMA_FILTER_SPARC:
389+
case LZMA_FILTER_ARM64:
390+
case LZMA_FILTER_RISCV:
376391
f->options = parse_filter_spec_bcj(state, spec);
377392
return f->options != NULL;
378393
default:
@@ -491,7 +506,9 @@ build_filter_spec(const lzma_filter *f)
491506
case LZMA_FILTER_IA64:
492507
case LZMA_FILTER_ARM:
493508
case LZMA_FILTER_ARMTHUMB:
494-
case LZMA_FILTER_SPARC: {
509+
case LZMA_FILTER_SPARC:
510+
case LZMA_FILTER_ARM64:
511+
case LZMA_FILTER_RISCV: {
495512
lzma_options_bcj *options = f->options;
496513
if (options) {
497514
ADD_FIELD(options, start_offset);
@@ -1544,6 +1561,8 @@ lzma_exec(PyObject *module)
15441561
ADD_INT_PREFIX_MACRO(module, FILTER_ARMTHUMB);
15451562
ADD_INT_PREFIX_MACRO(module, FILTER_SPARC);
15461563
ADD_INT_PREFIX_MACRO(module, FILTER_POWERPC);
1564+
ADD_INT_PREFIX_MACRO(module, FILTER_ARM64);
1565+
ADD_INT_PREFIX_MACRO(module, FILTER_RISCV);
15471566
ADD_INT_PREFIX_MACRO(module, MF_HC3);
15481567
ADD_INT_PREFIX_MACRO(module, MF_HC4);
15491568
ADD_INT_PREFIX_MACRO(module, MF_BT2);

0 commit comments

Comments
 (0)