From 97faf4054322f37e01d0220bf7e8dd026da7b91d Mon Sep 17 00:00:00 2001 From: Jeroen Diederen Date: Thu, 13 Aug 2026 09:51:48 +0700 Subject: [PATCH 1/2] python: fix BSUnit ctypes binding Pass BSUnit as a ctypes union to bs_size_convert_to and add a conversion regression test. Also make the translation canary shell test POSIX-compatible. --- src/python/bytesize.py | 12 ++++++++++-- tests/canary_tests.sh.in | 8 ++++++-- tests/libbytesize_unittest.py | 4 ++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/python/bytesize.py b/src/python/bytesize.py index a441d1e5..01ae54ce 100644 --- a/src/python/bytesize.py +++ b/src/python/bytesize.py @@ -81,6 +81,12 @@ def get_error(err): c_bytesize.bs_clear_error(byref(err)) raise ex +class SizeUnit(ctypes.Union): + _fields_ = [ + ("bunit", ctypes.c_int), + ("dunit", ctypes.c_int), + ] + class SizeStruct(ctypes.Structure): @classmethod def new(cls): @@ -155,7 +161,9 @@ def cmp_bytes(self, b, ign_sgn): def convert_to(self, unit): err = POINTER(SizeErrorStruct)() - ret = c_bytesize.bs_size_convert_to(self, unit, byref(err)) + u = SizeUnit() + u.bunit = unit + ret = c_bytesize.bs_size_convert_to(self, u, byref(err)) get_error(err) ret = str(ret, "utf-8") return ret @@ -264,7 +272,7 @@ def __repr__(self): c_bytesize.bs_size_get_bytes_str.restype = ctypes.c_char_p c_bytesize.bs_size_get_bytes_str.argtypes = [POINTER(SizeStruct)] c_bytesize.bs_size_convert_to.restype = ctypes.c_char_p -c_bytesize.bs_size_convert_to.argtypes = [POINTER(SizeStruct), ctypes.c_int, POINTER(POINTER(SizeErrorStruct))] +c_bytesize.bs_size_convert_to.argtypes = [POINTER(SizeStruct), SizeUnit, POINTER(POINTER(SizeErrorStruct))] c_bytesize.bs_size_human_readable.restype = ctypes.c_char_p c_bytesize.bs_size_human_readable.argtypes = [POINTER(SizeStruct), ctypes.c_int, ctypes.c_int, ctypes.c_bool] diff --git a/tests/canary_tests.sh.in b/tests/canary_tests.sh.in index d5a58159..8b523735 100644 --- a/tests/canary_tests.sh.in +++ b/tests/canary_tests.sh.in @@ -6,8 +6,12 @@ if [ @WITH_PYTHON3@ != 1 ]; then exit 0 fi -DISTRO=`busctl get-property org.freedesktop.hostname1 /org/freedesktop/hostname1 org.freedesktop.hostname1 OperatingSystemCPEName | cut -d ":" -f 3` -if [ $DISTRO == "centos" -o $DISTRO == "enterprise_linux" ]; then +DISTRO=$(busctl get-property org.freedesktop.hostname1 \ + /org/freedesktop/hostname1 \ + org.freedesktop.hostname1 OperatingSystemCPEName | + cut -d ":" -f 3) + +if [ "$DISTRO" = "centos" ] || [ "$DISTRO" = "enterprise_linux" ]; then echo "Cannot run translations tests on CentOS/RHEL 7, skipping." exit 0 fi diff --git a/tests/libbytesize_unittest.py b/tests/libbytesize_unittest.py index 24dffebc..638af7e9 100755 --- a/tests/libbytesize_unittest.py +++ b/tests/libbytesize_unittest.py @@ -365,8 +365,8 @@ def testCmpBytes(self): #enddef def testConvertTo(self): - x = SizeStruct.new_from_str("1 KiB") - x.convert_to(KiB) + x = SizeStruct.new_from_str("1 MiB") + self.assertEqual(x.convert_to(KiB), "1024") #enddef def testDiv(self): From c40b0c7b297edc22d8bfa55f463a4be1b8db3cf5 Mon Sep 17 00:00:00 2001 From: Jeroen Diederen Date: Fri, 14 Aug 2026 09:48:11 +0700 Subject: [PATCH 2/2] python: address review comments --- src/python/bytesize.py | 5 ++++- tests/libbytesize_unittest.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/python/bytesize.py b/src/python/bytesize.py index 01ae54ce..8850498c 100644 --- a/src/python/bytesize.py +++ b/src/python/bytesize.py @@ -162,7 +162,10 @@ def cmp_bytes(self, b, ign_sgn): def convert_to(self, unit): err = POINTER(SizeErrorStruct)() u = SizeUnit() - u.bunit = unit + if unit >= KB: + u.dunit = unit + else: + u.bunit = unit ret = c_bytesize.bs_size_convert_to(self, u, byref(err)) get_error(err) ret = str(ret, "utf-8") diff --git a/tests/libbytesize_unittest.py b/tests/libbytesize_unittest.py index 638af7e9..e958866a 100755 --- a/tests/libbytesize_unittest.py +++ b/tests/libbytesize_unittest.py @@ -9,7 +9,7 @@ from locale_utils import get_avail_locales, missing_locales, requires_locales -from bytesize import KiB, GiB, ROUND_UP, ROUND_DOWN, ROUND_HALF_UP, OverflowError, InvalidSpecError +from bytesize import KiB, MB, GiB, ROUND_UP, ROUND_DOWN, ROUND_HALF_UP, OverflowError, InvalidSpecError # SizeStruct is part of the 'private' API and needs to be imported differently # when running from locally build tree and when using installed library @@ -367,6 +367,9 @@ def testCmpBytes(self): def testConvertTo(self): x = SizeStruct.new_from_str("1 MiB") self.assertEqual(x.convert_to(KiB), "1024") + + x = SizeStruct.new_from_str("1 GB") + self.assertEqual(x.convert_to(MB), "1000") #enddef def testDiv(self):