diff --git a/src/python/bytesize.py b/src/python/bytesize.py index a441d1e..8850498 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,12 @@ 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() + 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") return ret @@ -264,7 +275,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 d5a5815..8b52373 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 24dffeb..e958866 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 @@ -365,8 +365,11 @@ 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") + + x = SizeStruct.new_from_str("1 GB") + self.assertEqual(x.convert_to(MB), "1000") #enddef def testDiv(self):