File bitarray3-compat.patch of Package ganeti
From 65574a9c08415d48af9e2e42c37f4fae517c7319 Mon Sep 17 00:00:00 2001 From: Sascha Lucas <sascha_lucas@web.de> Date: Tue, 16 Sep 2025 14:03:00 +0200 Subject: [PATCH] python-compatibiliy: for bitarray >=3 Since bitarray >=3, a.search() returns an iterator[1]. Introduce a subclass to transparently support <3 and >=3 versions of bitarray. [1] https://github.com/ilanschnell/bitarray/blob/3fbff059c3157c0cf3049231340a31e7c200a6bc/doc/bitarray3.rst Signed-off-by: Sascha Lucas <sascha_lucas@web.de> --- Makefile.am | 1 + lib/network.py | 2 +- lib/utils/bitarray_compat.py | 61 +++++++++++++++++++ lib/utils/bitarrays.py | 2 +- .../legacy/ganeti.utils.bitarrays_unittest.py | 2 +- 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 lib/utils/bitarray_compat.py diff --git a/Makefile.am b/Makefile.am index 94cc5f606a..6d352d57fa 100644 --- a/Makefile.am +++ b/Makefile.am @@ -749,6 +749,7 @@ pytools_PYTHON = \ utils_PYTHON = \ lib/utils/__init__.py \ lib/utils/algo.py \ + lib/utils/bitarray_compat.py \ lib/utils/filelock.py \ lib/utils/hash.py \ lib/utils/io.py \ diff --git a/lib/network.py b/lib/network.py index 07f159a127..0529061dce 100644 --- a/lib/network.py +++ b/lib/network.py @@ -34,7 +34,7 @@ import ipaddress -from bitarray import bitarray +from ganeti.utils.bitarray_compat import bitarray from ganeti import errors diff --git a/lib/utils/bitarray_compat.py b/lib/utils/bitarray_compat.py new file mode 100644 index 0000000000..8318313b51 --- /dev/null +++ b/lib/utils/bitarray_compat.py @@ -0,0 +1,61 @@ +# +# + +# Copyright (C) 2025 the Ganeti project +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# 1. Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +""" +CompatBitarray: Subclass from bitarray.bitarray returns always a list for +a.search(...) +""" + +from typing import TypeAlias +from bitarray import bitarray as _BaseBitarray + + +class CompatBitarray(_BaseBitarray): + # constructor forwarding for immutable C-Extension-Types + def __new__( + cls: "type[CompatBitarray]", + *args, + **kwargs + ) -> "CompatBitarray": + return _BaseBitarray.__new__(cls, *args, **kwargs) + + def search(self, *args, **kwargs) -> list[int]: + """ + https://github.com/ilanschnell/bitarray/blob/master/doc/bitarray3.rst + """ + res = super().search(*args, **kwargs) + return res if isinstance(res, list) else list(res) + + +# Alias +# pylint: disable=C0103 +bitarray: TypeAlias = CompatBitarray + +__all__ = ["CompatBitarray", "bitarray"] diff --git a/lib/utils/bitarrays.py b/lib/utils/bitarrays.py index a635eef197..81441b8eb2 100644 --- a/lib/utils/bitarrays.py +++ b/lib/utils/bitarrays.py @@ -32,7 +32,7 @@ """ -from bitarray import bitarray +from ganeti.utils.bitarray_compat import bitarray from ganeti import errors diff --git a/test/py/legacy/ganeti.utils.bitarrays_unittest.py b/test/py/legacy/ganeti.utils.bitarrays_unittest.py index c9ba120d40..7d3503a35d 100755 --- a/test/py/legacy/ganeti.utils.bitarrays_unittest.py +++ b/test/py/legacy/ganeti.utils.bitarrays_unittest.py @@ -32,7 +32,7 @@ import unittest import testutils -from bitarray import bitarray +from ganeti.utils.bitarray_compat import bitarray from ganeti import errors from ganeti.utils import bitarrays