Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/python/bytesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand Down
8 changes: 6 additions & 2 deletions tests/canary_tests.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Comment thread
vojtechtrefny marked this conversation as resolved.
/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
Expand Down
9 changes: 6 additions & 3 deletions tests/libbytesize_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a check with a decimal unit.


x = SizeStruct.new_from_str("1 GB")
self.assertEqual(x.convert_to(MB), "1000")
#enddef

def testDiv(self):
Expand Down