Skip to content

Commit

Permalink
Merge pull request #75 from cclauss/patch-11
Browse files Browse the repository at this point in the history
 Some basic Python 3 compatibility in grumpy_tools
  • Loading branch information
alanjds authored Sep 3, 2018
2 parents 8456fee + 597787c commit 4a57242
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 23 deletions.
16 changes: 11 additions & 5 deletions grumpy-tools-src/grumpy_tools/benchcmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@

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

from __future__ import print_function

import argparse
import subprocess
import sys

try:
xrange # Python 2
except NameError:
xrange = range # Python 3

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


def _MergeResults(merged, results, benchmarks):
Expand Down Expand Up @@ -65,10 +71,10 @@ def _RunBenchmark(prog):
line = line.strip()
if not line:
continue
parts = line.split()
if len(parts) != 3:
try:
name, status, result = line.split()
except ValueError:
_Die('invalid benchmark output: {}', line)
name, status, result = parts
if status != 'PASSED':
_Die('benchmark failed: {}', line)
try:
Expand All @@ -82,7 +88,7 @@ def _RunBenchmark(prog):
def _Die(msg, *args):
if args:
msg = msg.format(*args)
print >> sys.stderr, msg
print(msg, file=sys.stderr)
sys.exit(1)


Expand Down
5 changes: 5 additions & 0 deletions grumpy-tools-src/grumpy_tools/compiler/imputil.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
from grumpy_tools.vendor.pythonparser import algorithm
from grumpy_tools.vendor.pythonparser import ast

try:
xrange # Python 2
except NameError:
xrange = range # Python 3


_NATIVE_MODULE_PREFIX = '__go__/'

Expand Down
8 changes: 7 additions & 1 deletion grumpy-tools-src/grumpy_tools/coverparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@

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

from __future__ import print_function

import collections
import re
import sys

try:
xrange # Python 2
except NameError:
xrange = range # Python 3

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

Expand All @@ -44,7 +50,7 @@ def main():
uncovered = _ParseCover(f)
for filename in sorted(uncovered.keys()):
for lineno in sorted(uncovered[filename]):
print '{}:{}'.format(filename, lineno)
print('{}:{}'.format(filename, lineno))


if __name__ == '__main__':
Expand Down
4 changes: 3 additions & 1 deletion grumpy-tools-src/grumpy_tools/diffrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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

from __future__ import print_function

import sys


Expand Down Expand Up @@ -76,7 +78,7 @@ def main():
if line.startswith('+++'):
filename = line.split()[1]
for n in _ReadHunks(buf):
print '{}:{}'.format(filename, n)
print('{}:{}'.format(filename, n))


if __name__ == '__main__':
Expand Down
18 changes: 10 additions & 8 deletions grumpy-tools-src/grumpy_tools/genmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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

from __future__ import print_function

import argparse
import os
import subprocess
Expand All @@ -29,29 +31,29 @@


def _PrintRule(target, prereqs, rules):
print '{}: {}'.format(target, ' '.join(prereqs))
print('{}: {}'.format(target, ' '.join(prereqs)))
if rules:
print '\t@mkdir -p $(@D)'
print('\t@mkdir -p $(@D)')
for rule in rules:
print '\t@{}'.format(rule)
print
print('\t@{}'.format(rule))
print()


def main(args):
try:
proc = subprocess.Popen('go env GOOS GOARCH', shell=True,
stdout=subprocess.PIPE)
except OSError as e:
print >> sys.stderr, str(e)
print(str(e), file=sys.stderr)
return 1
out, _ = proc.communicate()
if proc.returncode:
print >> sys.stderr, 'go exited with status: {}'.format(proc.returncode)
print('go exited with status: {}'.format(proc.returncode), file=sys.stderr)
return 1
goos, goarch = out.split()

if args.all_target:
print '{}:\n'.format(args.all_target)
print('{}:\n'.format(args.all_target))

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


if __name__ == '__main__':
Expand Down
5 changes: 5 additions & 0 deletions grumpy-tools-src/grumpy_tools/pydeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
from .compiler import imputil
from .compiler import util

try:
xrange # Python 2
except NameError:
xrange = range # Python 3


def main(script=None, modname=None, package_dir='', with_imports=False):
gopath = os.environ['GOPATH']
Expand Down
1 change: 1 addition & 0 deletions grumpy-tools-src/grumpy_tools/vendor/pythonparser/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
if sys.version_info[0] == 3:
unichr = chr
byte = lambda x: bytes([x])
long = int
else:
byte = chr

Expand Down
11 changes: 6 additions & 5 deletions grumpy-tools-src/tests/havingmainpkg/__main__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import sys
from __future__ import print_function


def main():
print '__name__ is', __name__
print '__package__ is', __package__
print('__name__ is', __name__)
print('__package__ is', __package__)
from . import SPAM
print(SPAM)


if __name__ == '__main__':
print '__name__ IS __main__'
print '__package__ is', __package__
print('__name__ IS __main__')
print('__package__ is', __package__)
main()
8 changes: 5 additions & 3 deletions grumpy-tools-src/tests/import_havingpkgmain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
print 'STARTING'
from __future__ import print_function

print('STARTING')
from havingmainpkg.__main__ import main
print 'IMPORTED'
print('IMPORTED')

main()
print 'CALLED main()'
print('CALLED main()')

0 comments on commit 4a57242

Please sign in to comment.