X Tutup
Skip to content

Commit 1d1a14d

Browse files
committed
Convert Python 2 idioms to Python 2/3-compatible ones.
- convert print, StringIO, except as, octals, izip - convert print statement to print function - convert StringIO to six.StringIO - remove usage of csv reader in Spec, in favor of simple regex - csv reader only does byte strings - convert 0755 octal literals to 0o755 - convert `except Foo, e` to `except Foo as e` - fix a few places `str` is used. - may need to switch everything to str later. - convert iteritems usages to use six.iteritems - fix urllib and HTMLParser - port metaclasses to use six.with_metaclass - More octal literal conversions for Python 2/3 - Fix a new octal literal. - Convert `basestring` to `six.string_types` - Convert xrange -> range - Fix various issues with encoding, iteritems, and Python3 semantics. - Convert contextlib.nested to explicitly nexted context managers. - Convert use of filter() to list comprehensions. - Replace reduce() with list comprehensions. - Clean up composite: replace inspect.ismethod() with callable() - Python 3 doesn't have "method" objects; inspect.ismethod returns False. - Need to use callable in Composite to make it work. - Update colify to use future division. - Fix zip() usages that need to be lists. - Python3: Use line-buffered logging instead of unbuffered. - Python3 raises an error with unbuffered I/O - See https://bugs.python.org/issue17404
1 parent 0331b08 commit 1d1a14d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+396
-323
lines changed

bin/spack

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
# License along with this program; if not, write to the Free Software
2525
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2626
##############################################################################
27+
from __future__ import print_function
28+
2729
import sys
2830
if (sys.version_info[0] > 2) or (sys.version_info[:2] < (2, 6)):
2931
v_info = sys.version_info[:3]
@@ -74,8 +76,8 @@ for pyc_file in orphaned_pyc_files:
7476
try:
7577
os.remove(pyc_file)
7678
except OSError as e:
77-
print ("WARNING: Spack may fail mysteriously. "
78-
"Couldn't remove orphaned .pyc file: %s" % pyc_file)
79+
print("WARNING: Spack may fail mysteriously. "
80+
"Couldn't remove orphaned .pyc file: %s" % pyc_file)
7981

8082
# If there is no working directory, use the spack prefix.
8183
try:

lib/spack/llnl/util/filesystem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ def change_sed_delimiter(old_delim, new_delim, *filenames):
175175
def set_install_permissions(path):
176176
"""Set appropriate permissions on the installed file."""
177177
if os.path.isdir(path):
178-
os.chmod(path, 0755)
178+
os.chmod(path, 0o755)
179179
else:
180-
os.chmod(path, 0644)
180+
os.chmod(path, 0o644)
181181

182182

183183
def copy_mode(src, dest):

lib/spack/llnl/util/lang.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import functools
2828
import collections
2929
import inspect
30+
from six import string_types
3031

3132
# Ignore emacs backups when listing modules
3233
ignore_modules = [r'^\.#', '~$']
@@ -80,7 +81,7 @@ def index_by(objects, *funcs):
8081
return objects
8182

8283
f = funcs[0]
83-
if isinstance(f, basestring):
84+
if isinstance(f, str):
8485
f = lambda x: getattr(x, funcs[0])
8586
elif isinstance(f, tuple):
8687
f = lambda x: tuple(getattr(x, p) for p in funcs[0])
@@ -326,7 +327,7 @@ def match_predicate(*args):
326327
"""
327328
def match(string):
328329
for arg in args:
329-
if isinstance(arg, basestring):
330+
if isinstance(arg, string_types):
330331
if re.search(arg, string):
331332
return True
332333
elif isinstance(arg, list) or isinstance(arg, tuple):

lib/spack/llnl/util/tty/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import termios
3030
import struct
3131
import traceback
32-
from StringIO import StringIO
32+
from six import StringIO
3333

3434
from llnl.util.tty.color import *
3535

@@ -93,7 +93,7 @@ def msg(message, *args, **kwargs):
9393
else:
9494
cwrite("@*b{%s==>} %s" % (st_text, cescape(message)))
9595
for arg in args:
96-
print indent + str(arg)
96+
print(indent + str(arg))
9797

9898

9999
def info(message, *args, **kwargs):
@@ -201,7 +201,7 @@ def get_yes_or_no(prompt, **kwargs):
201201
if not ans:
202202
result = default_value
203203
if result is None:
204-
print "Please enter yes or no."
204+
print("Please enter yes or no.")
205205
else:
206206
if ans == 'y' or ans == 'yes':
207207
result = True
@@ -239,7 +239,7 @@ def hline(label=None, **kwargs):
239239
out.write(label)
240240
out.write(suffix)
241241

242-
print out.getvalue()
242+
print(out.getvalue())
243243

244244

245245
def terminal_size():

lib/spack/llnl/util/tty/colify.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
"""
2626
Routines for printing columnar output. See colify() for more information.
2727
"""
28+
from __future__ import division
29+
2830
import os
2931
import sys
30-
from StringIO import StringIO
32+
from six import StringIO
3133

3234
from llnl.util.tty import terminal_size
3335
from llnl.util.tty.color import clen, cextra
@@ -64,18 +66,18 @@ def config_variable_cols(elts, console_width, padding, cols=0):
6466
# Get a bound on the most columns we could possibly have.
6567
# 'clen' ignores length of ansi color sequences.
6668
lengths = [clen(e) for e in elts]
67-
max_cols = max(1, console_width / (min(lengths) + padding))
69+
max_cols = max(1, console_width // (min(lengths) + padding))
6870
max_cols = min(len(elts), max_cols)
6971

7072
# Range of column counts to try. If forced, use the supplied value.
71-
col_range = [cols] if cols else xrange(1, max_cols + 1)
73+
col_range = [cols] if cols else range(1, max_cols + 1)
7274

7375
# Determine the most columns possible for the console width.
7476
configs = [ColumnConfig(c) for c in col_range]
7577
for i, length in enumerate(lengths):
7678
for conf in configs:
7779
if conf.valid:
78-
col = i / ((len(elts) + conf.cols - 1) / conf.cols)
80+
col = i // ((len(elts) + conf.cols - 1) // conf.cols)
7981
p = padding if col < (conf.cols - 1) else 0
8082

8183
if conf.widths[col] < (length + p):
@@ -107,7 +109,7 @@ def config_uniform_cols(elts, console_width, padding, cols=0):
107109
# 'clen' ignores length of ansi color sequences.
108110
max_len = max(clen(e) for e in elts) + padding
109111
if cols == 0:
110-
cols = max(1, console_width / max_len)
112+
cols = max(1, console_width // max_len)
111113
cols = min(len(elts), cols)
112114

113115
config = ColumnConfig(cols)
@@ -193,12 +195,12 @@ def colify(elts, **options):
193195
raise ValueError("method must be one of: " + allowed_methods)
194196

195197
cols = config.cols
196-
rows = (len(elts) + cols - 1) / cols
198+
rows = (len(elts) + cols - 1) // cols
197199
rows_last_col = len(elts) % rows
198200

199-
for row in xrange(rows):
201+
for row in range(rows):
200202
output.write(" " * indent)
201-
for col in xrange(cols):
203+
for col in range(cols):
202204
elt = col * rows + row
203205
width = config.widths[col] + cextra(elts[elt])
204206
if col < cols - 1:
@@ -233,7 +235,7 @@ def colify_table(table, **options):
233235
columns = len(table[0])
234236

235237
def transpose():
236-
for i in xrange(columns):
238+
for i in range(columns):
237239
for row in table:
238240
yield row[i]
239241

lib/spack/llnl/util/tty/log.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,12 @@ def __exit__(self, exc_type, exc_val, exc_tb):
165165
self.p.join(60.0) # 1 minute to join the child
166166

167167
def _spawn_writing_daemon(self, read, input_stream):
168-
# Parent: read from child, skip the with block.
169-
read_file = os.fdopen(read, 'r', 0)
168+
# This is the Parent: read from child, skip the with block.
169+
170+
# Use line buffering (3rd param = 1) since Python 3 has a bug
171+
# that prevents unbuffered text I/O.
172+
read_file = os.fdopen(read, 'r', 1)
173+
170174
with open(self.filename, 'w') as log_file:
171175
with keyboard_input(input_stream):
172176
while True:

lib/spack/spack/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
try:
9797
repo = spack.repository.RepoPath()
9898
sys.meta_path.append(repo)
99-
except spack.error.SpackError, e:
99+
except spack.error.SpackError as e:
100100
tty.die('while initializing Spack RepoPath:', e.message)
101101

102102

lib/spack/spack/architecture.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def find_compilers(self, *paths):
287287

288288
# ensure all the version calls we made are cached in the parent
289289
# process, as well. This speeds up Spack a lot.
290-
clist = reduce(lambda x, y: x + y, compiler_lists)
290+
clist = [comp for cl in compiler_lists for comp in cl]
291291
return clist
292292

293293
def find_compiler(self, cmp_cls, *path):

lib/spack/spack/build_environment.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import shutil
5858
import sys
5959
import traceback
60+
from six import iteritems
6061

6162
import llnl.util.lang as lang
6263
import llnl.util.tty as tty
@@ -310,7 +311,7 @@ def set_build_environment_variables(pkg, env, dirty=False):
310311
environment = compiler.environment
311312
if 'set' in environment:
312313
env_to_set = environment['set']
313-
for key, value in env_to_set.iteritems():
314+
for key, value in iteritems(env_to_set):
314315
env.set('SPACK_ENV_SET_%s' % key, value)
315316
env.set('%s' % key, value)
316317
# Let shell know which variables to set
@@ -322,8 +323,9 @@ def set_build_environment_variables(pkg, env, dirty=False):
322323
env.set('SPACK_COMPILER_EXTRA_RPATHS', extra_rpaths)
323324

324325
# Add bin directories from dependencies to the PATH for the build.
325-
bin_dirs = reversed(filter(os.path.isdir, [
326-
'%s/bin' % d.prefix for d in pkg.spec.dependencies(deptype='build')]))
326+
bin_dirs = reversed(
327+
[d.prefix.bin for d in pkg.spec.dependencies(deptype='build')
328+
if os.path.isdir(d.prefix.bin)])
327329
bin_dirs = filter_system_bin_paths(bin_dirs)
328330
for item in bin_dirs:
329331
env.prepend_path('PATH', item)

lib/spack/spack/build_systems/autotools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def _do_patch_config_guess(self):
146146
if config_guess is not None:
147147
try:
148148
check_call([config_guess], stdout=PIPE, stderr=PIPE)
149-
mod = stat(my_config_guess).st_mode & 0777 | S_IWUSR
149+
mod = stat(my_config_guess).st_mode & 0o777 | S_IWUSR
150150
os.chmod(my_config_guess, mod)
151151
shutil.copyfile(config_guess, my_config_guess)
152152
return True

0 commit comments

Comments
 (0)
X Tutup