Gentoo Archives: gentoo-portage-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-portage-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-portage-dev] [PATCH] localized_size(): fix rounding in Python 2.
Date: Wed, 06 Aug 2014 17:12:23
Message-Id: 1407345160-16866-1-git-send-email-mgorny@gentoo.org
1 In Python 2, the '/' division operator defaults to integer division when
2 given integer argument. Therefore, the calculated size is trimmed before
3 we attempt to round it up. Instead, convert it to float first to
4 guarantee floating point division.
5 ---
6 pym/portage/localization.py | 2 +-
7 1 file changed, 1 insertion(+), 1 deletion(-)
8
9 diff --git a/pym/portage/localization.py b/pym/portage/localization.py
10 index 7d30b59..77417ba 100644
11 --- a/pym/portage/localization.py
12 +++ b/pym/portage/localization.py
13 @@ -35,6 +35,6 @@ def localized_size(num_bytes):
14 """
15
16 # always round up, so that small files don't end up as '0 KiB'
17 - num_kib = math.ceil(num_bytes / 1024)
18 + num_kib = math.ceil(float(num_bytes) / 1024)
19 formatted_num = locale.format('%d', num_kib, grouping=True)
20 return (_unicode_decode(formatted_num, encoding=_encodings['stdio']) + ' KiB')
21 --
22 2.0.4

Replies