Skip to content

Commit

Permalink
Run futurize --stage1 to make safe changes for python 3 compatibili…
Browse files Browse the repository at this point in the history
…ty. (#6063)

### Problem
First stage of [Python 3 port](#6062)

### Solution
Stage 1 of the futurize tool only makes safe changes, and it keeps the code base at Python 2. See http://python-future.org/futurize.html#forwards-conversion-stage1. Stage 2 will later convert this to actual Python 3 code.

This command was ran on
- src/python
- test/python
- contrib
- examples
- pants-plugins
- build-support/bin/check-header-helper.py

(not ran on testprojects)
  • Loading branch information
Eric-Arellano authored and Stu Hood committed Jul 3, 2018
1 parent ff4162d commit 5a4d7e9
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 32 deletions.
14 changes: 8 additions & 6 deletions build-support/bin/check_header_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#
# usage: check_header_helper.py dir1 [ dir2 [ ... ] ]

from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)
import os
import re
import sys
Expand Down Expand Up @@ -61,12 +63,12 @@ def main():
for directory in dirs:
failed_files.extend(check_dir(directory))
if failed_files:
print 'ERROR: All .py files other than __init__.py should start with the following header:'
print
print EXPECTED_HEADER
print '---'
print 'The following {} file(s) do not conform:'.format(len(failed_files))
print ' {}'.format('\n '.join(failed_files))
print('ERROR: All .py files other than __init__.py should start with the following header:')
print()
print(EXPECTED_HEADER)
print('---')
print('The following {} file(s) do not conform:'.format(len(failed_files)))
print(' {}'.format('\n '.join(failed_files)))
sys.exit(1)

if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def is_non_synthetic_python_target(target):

@staticmethod
def is_python_target(target):
return isinstance(target, (PythonTarget,))
return isinstance(target, PythonTarget)

def _calculate_python_sources(self, targets):
"""Generate a set of source files from the given targets."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ def _emit_package_descriptor(node_task, target, results_dir, node_paths):
else:
package = {}

if not package.has_key('name'):
if 'name' not in package:
package['name'] = target.package_name
elif package['name'] != target.package_name:
raise TaskError('Package name in the corresponding package.json is not the same '
'as the BUILD target name for {}'.format(target.address.reference()))

if not package.has_key('version'):
if 'version' not in package:
package['version'] = '0.0.0'

# TODO(Chris Pesto): Preserve compatibility with normal package.json files by dropping existing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def build_exception_map(cls, tokens):
if token_start[0] == token_end[0]:
exception_ranges[token_start[0]].append((token_start[1], token_end[1]))
else:
exception_ranges[token_start[0]].append((token_start[1], sys.maxint))
exception_ranges[token_start[0]].append((token_start[1], sys.maxsize))
for line in range(token_start[0] + 1, token_end[0]):
exception_ranges[line].append((0, sys.maxint))
exception_ranges[line].append((0, sys.maxsize))
exception_ranges[token_end[0]].append((0, token_end[1]))
return exception_ranges

Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/jvm/tasks/junit_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class JUnitRun(PartitionedTestRunnerTaskMixin, JvmToolTaskMixin, JvmTask):
def implementation_version(cls):
return super(JUnitRun, cls).implementation_version() + [('JUnitRun', 3)]

_BATCH_ALL = sys.maxint
_BATCH_ALL = sys.maxsize

@classmethod
def register_options(cls, register):
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/jvm/tasks/jvm_dependency_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def creator(target):
try:
with open(self.nodes_json(vt.results_dir)) as fp:
return Node.from_cacheable_dict(json.load(fp),
lambda spec: self.context.resolve(spec).__iter__().next())
lambda spec: next(self.context.resolve(spec).__iter__()))
except Exception:
self.context.log.warn("Can't deserialize json for target {}".format(target))
return Node(target.concrete_derived_from)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def merged(cls, report_test_suites, error_on_conflict=True, logger=None):
'using first result:\n -> {}'.format(suite_name,
', '.join(s.file for s in suites),
'\n '.join(map(str, cases))))
case = iter(cases).next()
case = next(iter(cases))
tests += 1
time += case.time
if case.error:
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/backend/project_info/tasks/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,9 @@ def iter_transitive_jars(jar_lib):
if hasattr(current_target, 'test_platform'):
info['test_platform'] = current_target.test_platform.name

info['roots'] = map(lambda (source_root, package_prefix): {
'source_root': source_root,
'package_prefix': package_prefix
info['roots'] = map(lambda source_root_package_prefix: {
'source_root': source_root_package_prefix[0],
'package_prefix': source_root_package_prefix[1]
}, self._source_roots_for_target(current_target))

if classpath_products:
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/base/exiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def exit_and_fail(self, msg=None):

def handle_unhandled_exception(self, exc_class=None, exc=None, tb=None, add_newline=False):
"""Default sys.excepthook implementation for unhandled exceptions."""
exc_class = exc_class or sys.exc_type
exc = exc or sys.exc_value
tb = tb or sys.exc_traceback
exc_class = exc_class or sys.exc_info()[0]
exc = exc or sys.exc_info()[1]
tb = tb or sys.exc_info()[2]

def format_msg(print_backtrace=True):
msg = 'Exception caught: ({})\n'.format(type(exc))
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/base/worker_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def error(e):

def submit_next():
try:
self.submit_async_work(work_iter.next(), workunit_parent=workunit_parent,
self.submit_async_work(next(work_iter), workunit_parent=workunit_parent,
on_success=lambda x: submit_next(), on_failure=error)
except StopIteration:
done() # The success case.
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/engine/legacy/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def _eager_fileset_with_spec(spec_path, filespec, snapshot, include_dirs=False):
rel_include_globs = filespec['globs']

relpath_adjusted_filespec = FilesetRelPathWrapper.to_filespec(rel_include_globs, spec_path)
if filespec.has_key('exclude'):
if 'exclude' in filespec:
relpath_adjusted_filespec['exclude'] = [FilesetRelPathWrapper.to_filespec(e['globs'], spec_path)
for e in filespec['exclude']]

Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/util/osutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import logging
import os

from functools import reduce

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from collections import defaultdict
from contextlib import contextmanager
from functools import reduce

from pants.backend.jvm.subsystems.jvm_platform import JvmPlatformSettings
from pants.backend.jvm.targets.java_library import JavaLibrary
Expand Down
2 changes: 1 addition & 1 deletion tests/python/pants_test/backend/jvm/tasks/test_jvm_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def setUp(self):

context = self.context(target_roots=[self.t1, self.t2, self.t3])

self.classpath = [os.path.join(self.pants_workdir, entry) for entry in 'a', 'b']
self.classpath = [os.path.join(self.pants_workdir, entry) for entry in ('a', 'b')]
self.populate_runtime_classpath(context, self.classpath)

self.task = self.create_task(context)
Expand Down
1 change: 1 addition & 0 deletions tests/python/pants_test/net/http/test_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import SocketServer
import unittest
from contextlib import closing, contextmanager
from functools import reduce
from threading import Thread

import mock
Expand Down
3 changes: 1 addition & 2 deletions tests/python/pants_test/scm/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
unicode_literals, with_statement)

import os
import types
import unittest
from contextlib import contextmanager
from textwrap import dedent
Expand Down Expand Up @@ -144,7 +143,7 @@ def lstat(*components):
self.assertEquals(reader.Symlink, lstat('not-a-dir'))
self.assertEquals(reader.File, lstat('README'))
self.assertEquals(reader.Dir, lstat('dir'))
self.assertEquals(types.NoneType, lstat('nope-not-here'))
self.assertEquals(type(None), lstat('nope-not-here'))

def test_readlink(self):
reader = self.git.repo_reader(self.initial_rev)
Expand Down
12 changes: 6 additions & 6 deletions tests/python/pants_test/util/test_contextutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def test_open_zip_raises_exception_on_falsey_paths(self):
falsey = (None, '', False)
for invalid in falsey:
with self.assertRaises(InvalidZipPath):
open_zip(invalid).gen.next()
next(open_zip(invalid).gen)

def test_open_zip_returns_realpath_on_badzipfile(self):
# In case of file corruption, deleting a Pants-constructed symlink would not resolve the error.
Expand All @@ -204,7 +204,7 @@ def test_open_zip_returns_realpath_on_badzipfile(self):
os.symlink(not_zip.name, file_symlink)
self.assertEquals(os.path.realpath(file_symlink), os.path.realpath(not_zip.name))
with self.assertRaisesRegexp(zipfile.BadZipfile, r'{}'.format(not_zip.name)):
open_zip(file_symlink).gen.next()
next(open_zip(file_symlink).gen)

@contextmanager
def _stdio_as_tempfiles(self):
Expand Down Expand Up @@ -295,11 +295,11 @@ def test_signal_handler_as(self):
])

def test_permissions(self):
with temporary_file(permissions=0700) as f:
self.assertEquals(0700, os.stat(f.name)[0] & 0777)
with temporary_file(permissions=0o700) as f:
self.assertEquals(0o700, os.stat(f.name)[0] & 0o777)

with temporary_dir(permissions=0644) as path:
self.assertEquals(0644, os.stat(path)[0] & 0777)
with temporary_dir(permissions=0o644) as path:
self.assertEquals(0o644, os.stat(path)[0] & 0o777)

def test_exception_logging(self):
fake_logger = mock.Mock()
Expand Down

0 comments on commit 5a4d7e9

Please sign in to comment.