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] Use __future__ to enable floating-point division of integers in Python 2.
Date: Wed, 06 Aug 2014 17:25:50
Message-Id: 1407345968-20693-1-git-send-email-mgorny@gentoo.org
1 In Python 2, the division ('/') operator defaults to integer
2 (truncating) division when given integer argument. In Python 3, it
3 performs floating-point division unconditionally instead. To overcome
4 this difference and get a consistent behavior, integers were converted
5 to floats explicitly in a few places.
6
7 Instead, use a simpler 'from __future__ import division' statement that
8 enables floating-point division globally in Python 2.
9 ---
10 pym/portage/localization.py | 4 +++-
11 pym/portage/output.py | 6 ++++--
12 pym/portage/util/_eventloop/EventLoop.py | 6 ++++--
13 pym/portage/util/_eventloop/PollSelectAdapter.py | 4 +++-
14 4 files changed, 14 insertions(+), 6 deletions(-)
15
16 diff --git a/pym/portage/localization.py b/pym/portage/localization.py
17 index 77417ba..2db4b7a 100644
18 --- a/pym/portage/localization.py
19 +++ b/pym/portage/localization.py
20 @@ -2,6 +2,8 @@
21 # Copyright 2004-2014 Gentoo Foundation
22 # Distributed under the terms of the GNU General Public License v2
23
24 +from __future__ import division
25 +
26 import locale
27 import math
28
29 @@ -35,6 +37,6 @@ def localized_size(num_bytes):
30 """
31
32 # always round up, so that small files don't end up as '0 KiB'
33 - num_kib = math.ceil(float(num_bytes) / 1024)
34 + num_kib = math.ceil(num_bytes / 1024)
35 formatted_num = locale.format('%d', num_kib, grouping=True)
36 return (_unicode_decode(formatted_num, encoding=_encodings['stdio']) + ' KiB')
37 diff --git a/pym/portage/output.py b/pym/portage/output.py
38 index cd660ac..3b05c92 100644
39 --- a/pym/portage/output.py
40 +++ b/pym/portage/output.py
41 @@ -3,6 +3,8 @@
42
43 __docformat__ = "epytext"
44
45 +from __future__ import division
46 +
47 import errno
48 import io
49 import formatter
50 @@ -778,14 +780,14 @@ class TermProgressBar(ProgressBar):
51 "<=>" + ((max_bar_width - bar_width) * " ") + "]")
52 return image
53 else:
54 - percentage = int(100 * float(curval) / maxval)
55 + percentage = int(100 * curval / maxval)
56 max_bar_width = bar_space - 1
57 _percent = ("%d%% " % percentage).rjust(percentage_str_width)
58 image = "%s%s" % (self._desc, _percent)
59
60 if cols < min_columns:
61 return image
62 - offset = float(curval) / maxval
63 + offset = curval / maxval
64 bar_width = int(offset * max_bar_width)
65 image = image + "[" + (bar_width * "=") + \
66 ">" + ((max_bar_width - bar_width) * " ") + "]"
67 diff --git a/pym/portage/util/_eventloop/EventLoop.py b/pym/portage/util/_eventloop/EventLoop.py
68 index 9ffcc74..d16539f 100644
69 --- a/pym/portage/util/_eventloop/EventLoop.py
70 +++ b/pym/portage/util/_eventloop/EventLoop.py
71 @@ -1,6 +1,8 @@
72 # Copyright 1999-2013 Gentoo Foundation
73 # Distributed under the terms of the GNU General Public License v2
74
75 +from __future__ import division
76 +
77 import errno
78 import logging
79 import os
80 @@ -211,7 +213,7 @@ class EventLoop(object):
81 if timeout is None:
82 wait_timeout = None
83 else:
84 - wait_timeout = float(timeout) / 1000
85 + wait_timeout = timeout / 1000
86 # NOTE: In order to avoid a possible infinite wait when
87 # wait_timeout is None, the previous _run_timeouts()
88 # call must have returned False *with* _thread_condition
89 @@ -657,6 +659,6 @@ class _epoll_adapter(object):
90 if timeout is None or timeout < 0:
91 timeout = -1
92 elif timeout != 0:
93 - timeout = float(timeout) / 1000
94 + timeout = timeout / 1000
95
96 return self._epoll_obj.poll(timeout)
97 diff --git a/pym/portage/util/_eventloop/PollSelectAdapter.py b/pym/portage/util/_eventloop/PollSelectAdapter.py
98 index 244788c..e0223a2 100644
99 --- a/pym/portage/util/_eventloop/PollSelectAdapter.py
100 +++ b/pym/portage/util/_eventloop/PollSelectAdapter.py
101 @@ -1,6 +1,8 @@
102 # Copyright 1999-2012 Gentoo Foundation
103 # Distributed under the terms of the GNU General Public License v2
104
105 +from __future__ import division
106 +
107 from .PollConstants import PollConstants
108 import select
109
110 @@ -64,7 +66,7 @@ class PollSelectAdapter(object):
111 if timeout is not None and timeout < 0:
112 timeout = None
113 if timeout is not None:
114 - select_args.append(float(timeout) / 1000)
115 + select_args.append(timeout / 1000)
116
117 select_events = select.select(*select_args)
118 poll_events = []
119 --
120 2.0.4

Replies