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 v2] Use __future__ to enable floating-point division of integers in Python 2.
Date: Mon, 11 Aug 2014 20:20:59
Message-Id: 1407746609-29354-1-git-send-email-mgorny@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH] Use __future__ to enable floating-point division of integers in Python 2. by "Michał Górny"
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. Use it in all
9 relevant files to get a consistent behavior, and use '//' appropriately
10 whenever integer division is desired.
11
12 X-Pull-Request: https://github.com/gentoo/portage/pull/2
13 ---
14 pym/_emerge/Scheduler.py | 2 +-
15 pym/_emerge/actions.py | 2 +-
16 pym/_emerge/depgraph.py | 4 ++--
17 pym/_emerge/sync/old_tree_timestamp.py | 10 ++++++----
18 pym/portage/_emirrordist/FetchTask.py | 4 +++-
19 pym/portage/_sets/dbapi.py | 2 ++
20 pym/portage/cache/sqlite.py | 4 ++--
21 pym/portage/dbapi/vartree.py | 4 ++--
22 pym/portage/localization.py | 2 ++
23 pym/portage/output.py | 6 ++++--
24 pym/portage/util/_eventloop/EventLoop.py | 6 ++++--
25 pym/portage/util/_eventloop/PollSelectAdapter.py | 4 +++-
26 12 files changed, 32 insertions(+), 18 deletions(-)
27
28 diff --git a/pym/_emerge/Scheduler.py b/pym/_emerge/Scheduler.py
29 index dd268f7..d6db311 100644
30 --- a/pym/_emerge/Scheduler.py
31 +++ b/pym/_emerge/Scheduler.py
32 @@ -1,7 +1,7 @@
33 # Copyright 1999-2014 Gentoo Foundation
34 # Distributed under the terms of the GNU General Public License v2
35
36 -from __future__ import print_function, unicode_literals
37 +from __future__ import division, print_function, unicode_literals
38
39 from collections import deque
40 import gc
41 diff --git a/pym/_emerge/actions.py b/pym/_emerge/actions.py
42 index b935139..fd2ad89 100644
43 --- a/pym/_emerge/actions.py
44 +++ b/pym/_emerge/actions.py
45 @@ -1,7 +1,7 @@
46 # Copyright 1999-2014 Gentoo Foundation
47 # Distributed under the terms of the GNU General Public License v2
48
49 -from __future__ import print_function, unicode_literals
50 +from __future__ import division, print_function, unicode_literals
51
52 import errno
53 import logging
54 diff --git a/pym/_emerge/depgraph.py b/pym/_emerge/depgraph.py
55 index acb1db1..a10297a 100644
56 --- a/pym/_emerge/depgraph.py
57 +++ b/pym/_emerge/depgraph.py
58 @@ -1,7 +1,7 @@
59 # Copyright 1999-2014 Gentoo Foundation
60 # Distributed under the terms of the GNU General Public License v2
61
62 -from __future__ import print_function, unicode_literals
63 +from __future__ import division, print_function, unicode_literals
64
65 import collections
66 import errno
67 @@ -8446,7 +8446,7 @@ def _backtrack_depgraph(settings, trees, myopts, myparams, myaction, myfiles, sp
68 debug = "--debug" in myopts
69 mydepgraph = None
70 max_retries = myopts.get('--backtrack', 10)
71 - max_depth = max(1, (max_retries + 1) / 2)
72 + max_depth = max(1, (max_retries + 1) // 2)
73 allow_backtracking = max_retries > 0
74 backtracker = Backtracker(max_depth)
75 backtracked = 0
76 diff --git a/pym/_emerge/sync/old_tree_timestamp.py b/pym/_emerge/sync/old_tree_timestamp.py
77 index 9b35aed..8411cae 100644
78 --- a/pym/_emerge/sync/old_tree_timestamp.py
79 +++ b/pym/_emerge/sync/old_tree_timestamp.py
80 @@ -1,6 +1,8 @@
81 # Copyright 2010 Gentoo Foundation
82 # Distributed under the terms of the GNU General Public License v2
83
84 +from __future__ import division
85 +
86 import locale
87 import logging
88 import time
89 @@ -27,16 +29,16 @@ def whenago(seconds):
90 out = []
91
92 if sec > 60:
93 - mins = sec / 60
94 + mins = sec // 60
95 sec = sec % 60
96 if mins > 60:
97 - hrs = mins / 60
98 + hrs = mins // 60
99 mins = mins % 60
100 if hrs > 24:
101 - days = hrs / 24
102 + days = hrs // 24
103 hrs = hrs % 24
104 if days > 365:
105 - years = days / 365
106 + years = days // 365
107 days = days % 365
108
109 if years:
110 diff --git a/pym/portage/_emirrordist/FetchTask.py b/pym/portage/_emirrordist/FetchTask.py
111 index 66c41c1..20e93fc 100644
112 --- a/pym/portage/_emirrordist/FetchTask.py
113 +++ b/pym/portage/_emirrordist/FetchTask.py
114 @@ -1,6 +1,8 @@
115 # Copyright 2013 Gentoo Foundation
116 # Distributed under the terms of the GNU General Public License v2
117
118 +from __future__ import division
119 +
120 import collections
121 import errno
122 import logging
123 @@ -242,7 +244,7 @@ class FetchTask(CompositeTask):
124 remaining_tries = self.config.options.tries - len(self._tried_uris)
125 if remaining_tries > 0:
126
127 - if remaining_tries <= self.config.options.tries / 2:
128 + if remaining_tries <= self.config.options.tries // 2:
129 while self._primaryuri_stack:
130 uri = self._primaryuri_stack.pop()
131 if uri not in self._tried_uris:
132 diff --git a/pym/portage/_sets/dbapi.py b/pym/portage/_sets/dbapi.py
133 index 384fb3a..dc846fc 100644
134 --- a/pym/portage/_sets/dbapi.py
135 +++ b/pym/portage/_sets/dbapi.py
136 @@ -1,6 +1,8 @@
137 # Copyright 2007-2012 Gentoo Foundation
138 # Distributed under the terms of the GNU General Public License v2
139
140 +from __future__ import division
141 +
142 import time
143
144 from portage import os
145 diff --git a/pym/portage/cache/sqlite.py b/pym/portage/cache/sqlite.py
146 index 42a2399..310ac94 100644
147 --- a/pym/portage/cache/sqlite.py
148 +++ b/pym/portage/cache/sqlite.py
149 @@ -1,7 +1,7 @@
150 # Copyright 1999-2014 Gentoo Foundation
151 # Distributed under the terms of the GNU General Public License v2
152
153 -from __future__ import unicode_literals
154 +from __future__ import division, unicode_literals
155
156 import re
157 import sys
158 @@ -174,7 +174,7 @@ class database(fs_template.FsBased):
159 cursor.execute("PRAGMA page_size")
160 page_size=int(cursor.fetchone()[0])
161 # number of pages, sqlite default is 2000
162 - cache_size = cache_bytes / page_size
163 + cache_size = cache_bytes // page_size
164 cursor.execute("PRAGMA cache_size = %d" % cache_size)
165 cursor.execute("PRAGMA cache_size")
166 actual_cache_size = int(cursor.fetchone()[0])
167 diff --git a/pym/portage/dbapi/vartree.py b/pym/portage/dbapi/vartree.py
168 index 2086d4c..5b947dd 100644
169 --- a/pym/portage/dbapi/vartree.py
170 +++ b/pym/portage/dbapi/vartree.py
171 @@ -1,7 +1,7 @@
172 # Copyright 1998-2014 Gentoo Foundation
173 # Distributed under the terms of the GNU General Public License v2
174
175 -from __future__ import unicode_literals
176 +from __future__ import division, unicode_literals
177
178 __all__ = [
179 "vardbapi", "vartree", "dblink"] + \
180 @@ -1043,7 +1043,7 @@ class vardbapi(dbapi):
181 from md5 import new as _new_hash
182
183 _hash_bits = 16
184 - _hex_chars = int(_hash_bits / 4)
185 + _hex_chars = _hash_bits // 4
186
187 def __init__(self, vardb):
188 self._vardb = vardb
189 diff --git a/pym/portage/localization.py b/pym/portage/localization.py
190 index 7d30b59..2db4b7a 100644
191 --- a/pym/portage/localization.py
192 +++ b/pym/portage/localization.py
193 @@ -2,6 +2,8 @@
194 # Copyright 2004-2014 Gentoo Foundation
195 # Distributed under the terms of the GNU General Public License v2
196
197 +from __future__ import division
198 +
199 import locale
200 import math
201
202 diff --git a/pym/portage/output.py b/pym/portage/output.py
203 index cd660ac..485f4e4 100644
204 --- a/pym/portage/output.py
205 +++ b/pym/portage/output.py
206 @@ -1,6 +1,8 @@
207 # Copyright 1998-2014 Gentoo Foundation
208 # Distributed under the terms of the GNU General Public License v2
209
210 +from __future__ import division
211 +
212 __docformat__ = "epytext"
213
214 import errno
215 @@ -778,14 +780,14 @@ class TermProgressBar(ProgressBar):
216 "<=>" + ((max_bar_width - bar_width) * " ") + "]")
217 return image
218 else:
219 - percentage = int(100 * float(curval) / maxval)
220 + percentage = int(100 * curval / maxval)
221 max_bar_width = bar_space - 1
222 _percent = ("%d%% " % percentage).rjust(percentage_str_width)
223 image = "%s%s" % (self._desc, _percent)
224
225 if cols < min_columns:
226 return image
227 - offset = float(curval) / maxval
228 + offset = curval / maxval
229 bar_width = int(offset * max_bar_width)
230 image = image + "[" + (bar_width * "=") + \
231 ">" + ((max_bar_width - bar_width) * " ") + "]"
232 diff --git a/pym/portage/util/_eventloop/EventLoop.py b/pym/portage/util/_eventloop/EventLoop.py
233 index 9ffcc74..d16539f 100644
234 --- a/pym/portage/util/_eventloop/EventLoop.py
235 +++ b/pym/portage/util/_eventloop/EventLoop.py
236 @@ -1,6 +1,8 @@
237 # Copyright 1999-2013 Gentoo Foundation
238 # Distributed under the terms of the GNU General Public License v2
239
240 +from __future__ import division
241 +
242 import errno
243 import logging
244 import os
245 @@ -211,7 +213,7 @@ class EventLoop(object):
246 if timeout is None:
247 wait_timeout = None
248 else:
249 - wait_timeout = float(timeout) / 1000
250 + wait_timeout = timeout / 1000
251 # NOTE: In order to avoid a possible infinite wait when
252 # wait_timeout is None, the previous _run_timeouts()
253 # call must have returned False *with* _thread_condition
254 @@ -657,6 +659,6 @@ class _epoll_adapter(object):
255 if timeout is None or timeout < 0:
256 timeout = -1
257 elif timeout != 0:
258 - timeout = float(timeout) / 1000
259 + timeout = timeout / 1000
260
261 return self._epoll_obj.poll(timeout)
262 diff --git a/pym/portage/util/_eventloop/PollSelectAdapter.py b/pym/portage/util/_eventloop/PollSelectAdapter.py
263 index 244788c..e0223a2 100644
264 --- a/pym/portage/util/_eventloop/PollSelectAdapter.py
265 +++ b/pym/portage/util/_eventloop/PollSelectAdapter.py
266 @@ -1,6 +1,8 @@
267 # Copyright 1999-2012 Gentoo Foundation
268 # Distributed under the terms of the GNU General Public License v2
269
270 +from __future__ import division
271 +
272 from .PollConstants import PollConstants
273 import select
274
275 @@ -64,7 +66,7 @@ class PollSelectAdapter(object):
276 if timeout is not None and timeout < 0:
277 timeout = None
278 if timeout is not None:
279 - select_args.append(float(timeout) / 1000)
280 + select_args.append(timeout / 1000)
281
282 select_events = select.select(*select_args)
283 poll_events = []
284 --
285 2.0.4