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

Replies