Skip to content

Commit 4a57242

Browse files
authored
Merge pull request #75 from cclauss/patch-11
Some basic Python 3 compatibility in grumpy_tools
2 parents 8456fee + 597787c commit 4a57242

File tree

9 files changed

+53
-23
lines changed

9 files changed

+53
-23
lines changed

grumpy-tools-src/grumpy_tools/benchcmp.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616

1717
"""Runs two benchmark programs and compares their results."""
1818

19+
from __future__ import print_function
20+
1921
import argparse
2022
import subprocess
2123
import sys
2224

25+
try:
26+
xrange # Python 2
27+
except NameError:
28+
xrange = range # Python 3
2329

2430
parser = argparse.ArgumentParser()
2531
parser.add_argument('prog1')
@@ -37,7 +43,7 @@ def main(args):
3743
_MergeResults(results2, _RunBenchmark(args.prog2), benchmarks)
3844
_MergeResults(results2, _RunBenchmark(args.prog2), benchmarks)
3945
for b in sorted(benchmarks):
40-
print b, '{:+.1%}'.format(results2[b] / results1[b] - 1)
46+
print(b, '{:+.1%}'.format(results2[b] / results1[b] - 1))
4147

4248

4349
def _MergeResults(merged, results, benchmarks):
@@ -65,10 +71,10 @@ def _RunBenchmark(prog):
6571
line = line.strip()
6672
if not line:
6773
continue
68-
parts = line.split()
69-
if len(parts) != 3:
74+
try:
75+
name, status, result = line.split()
76+
except ValueError:
7077
_Die('invalid benchmark output: {}', line)
71-
name, status, result = parts
7278
if status != 'PASSED':
7379
_Die('benchmark failed: {}', line)
7480
try:
@@ -82,7 +88,7 @@ def _RunBenchmark(prog):
8288
def _Die(msg, *args):
8389
if args:
8490
msg = msg.format(*args)
85-
print >> sys.stderr, msg
91+
print(msg, file=sys.stderr)
8692
sys.exit(1)
8793

8894

grumpy-tools-src/grumpy_tools/compiler/imputil.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
from grumpy_tools.vendor.pythonparser import algorithm
3131
from grumpy_tools.vendor.pythonparser import ast
3232

33+
try:
34+
xrange # Python 2
35+
except NameError:
36+
xrange = range # Python 3
37+
3338

3439
_NATIVE_MODULE_PREFIX = '__go__/'
3540

grumpy-tools-src/grumpy_tools/coverparse.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616

1717
"""Parse a Go coverage file and prints a message for lines missing coverage."""
1818

19+
from __future__ import print_function
20+
1921
import collections
2022
import re
2123
import sys
2224

25+
try:
26+
xrange # Python 2
27+
except NameError:
28+
xrange = range # Python 3
2329

2430
cover_re = re.compile(r'([^:]+):(\d+)\.\d+,(\d+).\d+ \d+ (\d+)$')
2531

@@ -44,7 +50,7 @@ def main():
4450
uncovered = _ParseCover(f)
4551
for filename in sorted(uncovered.keys()):
4652
for lineno in sorted(uncovered[filename]):
47-
print '{}:{}'.format(filename, lineno)
53+
print('{}:{}'.format(filename, lineno))
4854

4955

5056
if __name__ == '__main__':

grumpy-tools-src/grumpy_tools/diffrange.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
"""Convert a unified diff into a list of modified files and line numbers."""
1818

19+
from __future__ import print_function
20+
1921
import sys
2022

2123

@@ -76,7 +78,7 @@ def main():
7678
if line.startswith('+++'):
7779
filename = line.split()[1]
7880
for n in _ReadHunks(buf):
79-
print '{}:{}'.format(filename, n)
81+
print('{}:{}'.format(filename, n))
8082

8183

8284
if __name__ == '__main__':

grumpy-tools-src/grumpy_tools/genmake.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
"""Generate a Makefile for Python targets in a GOPATH directory."""
1818

19+
from __future__ import print_function
20+
1921
import argparse
2022
import os
2123
import subprocess
@@ -29,29 +31,29 @@
2931

3032

3133
def _PrintRule(target, prereqs, rules):
32-
print '{}: {}'.format(target, ' '.join(prereqs))
34+
print('{}: {}'.format(target, ' '.join(prereqs)))
3335
if rules:
34-
print '\t@mkdir -p $(@D)'
36+
print('\t@mkdir -p $(@D)')
3537
for rule in rules:
36-
print '\t@{}'.format(rule)
37-
print
38+
print('\t@{}'.format(rule))
39+
print()
3840

3941

4042
def main(args):
4143
try:
4244
proc = subprocess.Popen('go env GOOS GOARCH', shell=True,
4345
stdout=subprocess.PIPE)
4446
except OSError as e:
45-
print >> sys.stderr, str(e)
47+
print(str(e), file=sys.stderr)
4648
return 1
4749
out, _ = proc.communicate()
4850
if proc.returncode:
49-
print >> sys.stderr, 'go exited with status: {}'.format(proc.returncode)
51+
print('go exited with status: {}'.format(proc.returncode), file=sys.stderr)
5052
return 1
5153
goos, goarch = out.split()
5254

5355
if args.all_target:
54-
print '{}:\n'.format(args.all_target)
56+
print('{}:\n'.format(args.all_target))
5557

5658
gopath = os.path.normpath(args.dir)
5759
pkg_dir = os.path.join(gopath, 'pkg', '{}_{}'.format(goos, goarch))
@@ -80,7 +82,7 @@ def main(args):
8082
_PrintRule(ar_name, [go_file], [recipe.format(go_package, pkg_dir)])
8183
if args.all_target:
8284
_PrintRule(args.all_target, [ar_name], [])
83-
print '-include {}\n'.format(dep_file)
85+
print('-include {}\n'.format(dep_file))
8486

8587

8688
if __name__ == '__main__':

grumpy-tools-src/grumpy_tools/pydeps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
from .compiler import imputil
2424
from .compiler import util
2525

26+
try:
27+
xrange # Python 2
28+
except NameError:
29+
xrange = range # Python 3
30+
2631

2732
def main(script=None, modname=None, package_dir='', with_imports=False):
2833
gopath = os.environ['GOPATH']

grumpy-tools-src/grumpy_tools/vendor/pythonparser/lexer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
if sys.version_info[0] == 3:
1212
unichr = chr
1313
byte = lambda x: bytes([x])
14+
long = int
1415
else:
1516
byte = chr
1617

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import sys
1+
from __future__ import print_function
2+
23

34
def main():
4-
print '__name__ is', __name__
5-
print '__package__ is', __package__
5+
print('__name__ is', __name__)
6+
print('__package__ is', __package__)
67
from . import SPAM
78
print(SPAM)
89

910

1011
if __name__ == '__main__':
11-
print '__name__ IS __main__'
12-
print '__package__ is', __package__
12+
print('__name__ IS __main__')
13+
print('__package__ is', __package__)
1314
main()
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
print 'STARTING'
1+
from __future__ import print_function
2+
3+
print('STARTING')
24
from havingmainpkg.__main__ import main
3-
print 'IMPORTED'
5+
print('IMPORTED')
46

57
main()
6-
print 'CALLED main()'
8+
print('CALLED main()')

0 commit comments

Comments
 (0)