Skip to content

Commit

Permalink
Remove Python 2 handling of unicode (#7973)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Arellano committed Jun 29, 2019
1 parent dd11eac commit 7dea441
Show file tree
Hide file tree
Showing 52 changed files with 35 additions and 182 deletions.
Expand Up @@ -4,7 +4,6 @@
import json
import os

from future.utils import PY3
from pants.base.exceptions import TaskError
from pants.base.workunit import WorkUnitLabel
from pants.subsystem.subsystem import Subsystem
Expand Down Expand Up @@ -118,8 +117,7 @@ def resolve_target(self, node_task, target, results_dir, node_paths, resolve_loc
if self._get_target_from_package_name(target, package_name, file_path) is None:
raise TaskError('Local dependency in package.json not found in the build graph. '
'Check your BUILD file for missing dependencies. ["{}": {}]'.format(package_name, file_path))
mode = 'w' if PY3 else 'wb'
with open(package_json, mode) as package_json_file:
with open(package_json, 'w') as package_json_file:
json.dump(json_data, package_json_file, indent=2, separators=(',', ': '))
result, command = node_task.install_module(
target=target, install_optional=install_optional,
Expand Down Expand Up @@ -215,6 +213,5 @@ def _emit_package_descriptor(node_task, target, results_dir, node_paths):
'Adding {} to package.json for {}'.format(dependencies, package['name']))
package['dependencies'] = dependencies

mode = 'w' if PY3 else 'wb'
with open(package_json_path, mode) as fp:
with open(package_json_path, 'w') as fp:
json.dump(package, fp, indent=2)
1 change: 0 additions & 1 deletion contrib/node/src/python/pants/contrib/node/tasks/BUILD
Expand Up @@ -50,7 +50,6 @@ python_library(
name='node_repl',
sources=['node_repl.py'],
dependencies=[
'3rdparty/python:future',
':node_paths',
':node_task',
'src/python/pants/base:exceptions',
Expand Down
Expand Up @@ -4,7 +4,6 @@
import json
import os

from future.utils import PY3
from pants.base.exceptions import TaskError
from pants.task.repl_task_mixin import ReplTaskMixin
from pants.util.contextutil import pushd, temporary_dir
Expand Down Expand Up @@ -53,8 +52,7 @@ def launch_repl(self, targets):
else target.version for target in targets
}
}
mode = 'w' if PY3 else 'wb'
with open(package_json_path, mode) as fp:
with open(package_json_path, 'w') as fp:
json.dump(package, fp, indent=2)

args = self.get_passthru_args()
Expand Down
Expand Up @@ -83,7 +83,6 @@ python_tests(
name='node_task',
sources=['test_node_task.py'],
dependencies=[
'3rdparty/python:future',
'contrib/node/src/python/pants/contrib/node/targets:node_module',
'contrib/node/src/python/pants/contrib/node/targets:node_remote_module',
'contrib/node/src/python/pants/contrib/node/targets:node_test',
Expand Down
Expand Up @@ -6,7 +6,6 @@
import string
from textwrap import dedent

from future.utils import PY3
from pants.build_graph.target import Target
from pants.util.contextutil import pushd, temporary_dir
from pants_test.task_test_base import TaskTestBase
Expand Down Expand Up @@ -109,8 +108,7 @@ def test_execute_npm(self):
'proof': 'echo "42" > {}'.format(proof)
}
}
mode = 'w' if PY3 else 'wb'
with open(os.path.join(chroot, 'package.json'), mode) as fp:
with open(os.path.join(chroot, 'package.json'), 'w') as fp:
json.dump(package, fp)
with pushd(chroot):
returncode, _ = task.run_script(
Expand All @@ -135,8 +133,7 @@ def test_execute_yarnpkg(self):
'proof': 'echo "42" > {}'.format(proof)
}
}
mode = 'w' if PY3 else 'wb'
with open(os.path.join(chroot, 'package.json'), mode) as fp:
with open(os.path.join(chroot, 'package.json'), 'w') as fp:
json.dump(package, fp)
with pushd(chroot):
returncode, _ = task.run_script(
Expand Down
Expand Up @@ -3,7 +3,6 @@

python_library(
dependencies = [
'3rdparty/python:future',
'contrib/node/src/python/pants/contrib/node/subsystems/resolvers:node_resolver_base',
'src/python/pants/build_graph',
'src/python/pants/option',
Expand Down
Expand Up @@ -5,7 +5,6 @@
import os
import shutil

from future.utils import PY3
from pants.build_graph.injectables_mixin import InjectablesMixin
from pants.option.custom_types import target_option
from pants.subsystem.subsystem import Subsystem
Expand Down Expand Up @@ -50,8 +49,7 @@ def resolve_target(self, node_task, target, results_dir, node_paths):
'version': '0.0.0',
'main': main,
}
mode = 'w' if PY3 else 'wb'
with open(os.path.join(results_dir, 'package.json'), mode) as fp:
with open(os.path.join(results_dir, 'package.json'), 'w') as fp:
json.dump(package, fp, indent=2)

@property
Expand Down
1 change: 0 additions & 1 deletion examples/src/python/example/tensorflow_custom_op/BUILD
Expand Up @@ -8,7 +8,6 @@
python_library(
sources=['zero_out_custom_op.py'],
dependencies=[
'3rdparty/python:future',
'examples/3rdparty/python:tensorflow',
':tensorflow-zero-out-op-wrapper',
],
Expand Down
Expand Up @@ -2,16 +2,11 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import tensorflow as tf
from future.utils import PY3
# TODO: It would be great if we could maintain the example.tensorflow_custom_op package prefix for
# this python_dist()!
from wrap_lib.wrap_zero_out_op import zero_out_op_lib_path


# We make this a function in order to lazily load the op library.
def zero_out_module():
if PY3:
encoded_op_lib_path = zero_out_op_lib_path
else:
encoded_op_lib_path = zero_out_op_lib_path.encode()
return tf.load_op_library(encoded_op_lib_path)
return tf.load_op_library(zero_out_op_lib_path)
1 change: 0 additions & 1 deletion src/python/pants/backend/jvm/BUILD
Expand Up @@ -46,7 +46,6 @@ python_library(
sources=['ivy_utils.py'],
dependencies=[
'3rdparty/python/twitter/commons:twitter.common.collections',
'3rdparty/python:future',
'3rdparty/python:six',
':ivy_utils_resources',
'src/python/pants/backend/jvm/subsystems:jar_dependency_management',
Expand Down
4 changes: 1 addition & 3 deletions src/python/pants/backend/jvm/ivy_utils.py
Expand Up @@ -12,7 +12,6 @@
from functools import total_ordering

import six
from future.utils import PY3
from twitter.common.collections import OrderedSet

from pants.backend.jvm.subsystems.jar_dependency_management import (JarDependencyManagement,
Expand Down Expand Up @@ -366,8 +365,7 @@ def dump_to_file(cls, filename, resolutions_by_conf):
])

with safe_concurrent_creation(filename) as tmp_filename:
mode = 'w' if PY3 else 'wb'
with open(tmp_filename, mode) as f:
with open(tmp_filename, 'w') as f:
json.dump(res, f)


Expand Down
3 changes: 0 additions & 3 deletions src/python/pants/backend/jvm/tasks/BUILD
Expand Up @@ -274,7 +274,6 @@ python_library(
name = 'coursier_resolve',
sources = ['coursier_resolve.py'],
dependencies = [
'3rdparty/python:future',
':classpath_products',
':nailgun_task',
'src/python/pants/backend/jvm/tasks/coursier',
Expand Down Expand Up @@ -312,7 +311,6 @@ python_library(
name = 'jar_publish',
sources = ['jar_publish.py'],
dependencies = [
'3rdparty/python:future',
'3rdparty/python/twitter/commons:twitter.common.collections',
':jar_publish_resources',
':jar_task',
Expand Down Expand Up @@ -462,7 +460,6 @@ python_library(
name = 'jvm_dependency_usage',
sources = ['jvm_dependency_usage.py'],
dependencies = [
'3rdparty/python:future',
':jvm_dependency_analyzer',
'src/python/pants/backend/jvm/targets:jvm',
'src/python/pants/base:build_environment',
Expand Down
5 changes: 1 addition & 4 deletions src/python/pants/backend/jvm/tasks/coursier_resolve.py
Expand Up @@ -8,8 +8,6 @@
from collections import defaultdict
from urllib import parse

from future.utils import PY3

from pants.backend.jvm.ivy_utils import IvyUtils
from pants.backend.jvm.subsystems.jar_dependency_management import (JarDependencyManagement,
PinnedJarArtifactSet)
Expand Down Expand Up @@ -494,8 +492,7 @@ def get_transitive_resolved_jars(my_coord, resolved_jars):
compile_classpath.add_jars_for_targets([target], conf, jars_to_add)

def _populate_results_dir(self, vts_results_dir, results):
mode = 'w' if PY3 else 'wb'
with open(os.path.join(vts_results_dir, self.RESULT_FILENAME), mode) as f:
with open(os.path.join(vts_results_dir, self.RESULT_FILENAME), 'w') as f:
json.dump(results, f)

def _load_from_results_dir(self, compile_classpath, vts_results_dir,
Expand Down
4 changes: 1 addition & 3 deletions src/python/pants/backend/jvm/tasks/jar_publish.py
Expand Up @@ -11,7 +11,6 @@
from collections import OrderedDict, defaultdict, namedtuple
from copy import copy

from future.utils import PY3
from twitter.common.collections import OrderedSet

from pants.backend.jvm.ossrh_publication_metadata import OSSRHPublicationMetadata
Expand Down Expand Up @@ -746,8 +745,7 @@ def stage_artifacts(tgt, jar, version, tag, changelog):
coordinate(jar.org, jar.name), oldentry.version(), oldentry.sha, changelog)
# The stdout encoding can be detected as None when running without a tty (common in
# tests), in which case we want to force encoding with a unicode-supporting codec.
# In Py3, sys.stdout is a unicode stream.
sys.stdout.write(message) if PY3 else sys.stdout.write(message.encode('utf-8'))
sys.stdout.write(message)
if not self.confirm_push(coordinate(jar.org, jar.name), newentry.version()):
raise TaskError('User aborted push')

Expand Down
1 change: 0 additions & 1 deletion src/python/pants/backend/jvm/tasks/jvm_compile/BUILD
Expand Up @@ -36,7 +36,6 @@ python_library(
python_library(
sources = ['jvm_compile.py'],
dependencies = [
'3rdparty/python:future',
':compile_context',
':execution_graph',
':missing_dependency_finder',
Expand Down
Expand Up @@ -5,7 +5,6 @@
import os
from multiprocessing import cpu_count

from future.utils import PY2, PY3
from twitter.common.collections import OrderedSet

from pants.backend.jvm.subsystems.dependency_context import DependencyContext
Expand Down Expand Up @@ -39,7 +38,6 @@
safe_rmtree)
from pants.util.fileutil import create_size_estimators
from pants.util.memo import memoized_method, memoized_property
from pants.util.strutil import ensure_text


# Well known metadata file to register javac plugins.
Expand Down Expand Up @@ -273,7 +271,7 @@ def post_compile_extra_resources(self, compile_context):
target = compile_context.target

if isinstance(target, JavacPlugin):
result[_JAVAC_PLUGIN_INFO_FILE] = target.classname if PY3 else target.classname.decode('utf-8')
result[_JAVAC_PLUGIN_INFO_FILE] = target.classname
elif isinstance(target, AnnotationProcessor) and target.processors:
result[_PROCESSOR_INFO_FILE] = '{}\n'.format('\n'.join(p.strip() for p in target.processors))

Expand Down Expand Up @@ -311,11 +309,6 @@ def write_argsfile(self, ctx, args):
"""Write the argsfile for this context."""
with open(ctx.args_file, 'w') as fp:
for arg in args:
# NB: in Python 2, options are stored sometimes as bytes and sometimes as unicode in the OptionValueContainer.
# This is due to how Python 2 natively stores attributes as a map of `str` (aka `bytes`) to their value. So,
# the setattr() and getattr() functions sometimes use bytes.
if PY2:
arg = ensure_text(arg)
fp.write(arg)
fp.write('\n')

Expand Down
1 change: 0 additions & 1 deletion src/python/pants/backend/jvm/tasks/jvm_compile/rsc/BUILD
@@ -1,7 +1,6 @@
python_library(
dependencies = [
'3rdparty/python/twitter/commons:twitter.common.collections',
'3rdparty/python:future',
'src/python/pants/backend/jvm/subsystems:java',
'src/python/pants/backend/jvm/subsystems:jvm_platform',
'src/python/pants/backend/jvm/subsystems:rsc',
Expand Down
Expand Up @@ -6,8 +6,6 @@
import os
import re

from future.utils import PY3

from pants.backend.jvm.subsystems.dependency_context import DependencyContext # noqa
from pants.backend.jvm.subsystems.rsc import Rsc
from pants.backend.jvm.subsystems.shader import Shader
Expand Down Expand Up @@ -313,7 +311,7 @@ def work_for_vts_rsc(vts, ctx):
tgt, = vts.targets

if not hit_cache:
counter_val = str(counter()).rjust(counter.format_length(), ' ' if PY3 else b' ')
counter_val = str(counter()).rjust(counter.format_length(), ' ')
counter_str = '[{}/{}] '.format(counter_val, counter.size)
self.context.log.info(
counter_str,
Expand Down
5 changes: 1 addition & 4 deletions src/python/pants/backend/jvm/tasks/jvm_dependency_usage.py
Expand Up @@ -6,8 +6,6 @@
import sys
from collections import defaultdict, namedtuple

from future.utils import PY3

from pants.backend.jvm.targets.jar_library import JarLibrary
from pants.backend.jvm.tasks.jvm_dependency_analyzer import JvmDependencyAnalyzer
from pants.base.build_environment import get_buildroot
Expand Down Expand Up @@ -169,8 +167,7 @@ def creator(target):
transitive_deps = set(transitive_deps_by_target.get(target))
node = self.create_dep_usage_node(target, targets_by_file, transitive_deps)
vt = target_to_vts[target]
mode = 'w' if PY3 else 'wb'
with open(self.nodes_json(vt.results_dir), mode=mode) as fp:
with open(self.nodes_json(vt.results_dir), mode='w') as fp:
json.dump(node.to_cacheable_dict(), fp, indent=2, sort_keys=True)
vt.update()
return node
Expand Down
1 change: 0 additions & 1 deletion src/python/pants/backend/python/subsystems/BUILD
Expand Up @@ -3,7 +3,6 @@

python_library(
dependencies = [
'3rdparty/python:future',
'3rdparty/python:pex',
'3rdparty/python/twitter/commons:twitter.common.collections',
'src/python/pants/base:build_environment',
Expand Down
9 changes: 0 additions & 9 deletions src/python/pants/backend/python/subsystems/pex_build_util.py
Expand Up @@ -5,7 +5,6 @@
import os
from collections import defaultdict

from future.utils import PY2
from pex.fetcher import Fetcher
from pex.pex_builder import PEXBuilder
from pex.resolver import resolve
Expand Down Expand Up @@ -115,10 +114,6 @@ def dump_sources(builder, tgt, log):
log.debug(' Dumping sources: {}'.format(tgt))
for relpath in tgt.sources_relative_to_buildroot():
try:
# Necessary to avoid py_compile from trying to decode non-ascii source code into unicode.
# Python 3's py_compile can safely handle unicode in source files, meanwhile.
if PY2:
relpath = relpath.encode('utf-8')
dump_source(relpath)
except OSError:
log.error('Failed to copy {} for target {}'.format(relpath, tgt.address.spec))
Expand Down Expand Up @@ -301,10 +296,6 @@ def add_sources_from(self, tgt):
self._log.debug(' Dumping sources: {}'.format(tgt))
for relpath in tgt.sources_relative_to_buildroot():
try:
# Necessary to avoid py_compile from trying to decode non-ascii source code into unicode.
# Python 3's py_compile can safely handle unicode in source files, meanwhile.
if PY2:
relpath = relpath.encode('utf-8')
dump_source(relpath)
except OSError:
self._log.error('Failed to copy {} for target {}'.format(relpath, tgt.address.spec))
Expand Down
5 changes: 1 addition & 4 deletions src/python/pants/backend/python/tasks/pytest_run.py
Expand Up @@ -14,8 +14,6 @@
from io import StringIO
from textwrap import dedent

from future.utils import PY3

from pants.backend.python.targets.python_tests import PythonTests
from pants.backend.python.tasks.gather_sources import GatherSources
from pants.backend.python.tasks.pytest_prep import PytestPrep
Expand Down Expand Up @@ -383,8 +381,7 @@ def _pants_pytest_plugin_args(self, sources_map):
# it to pick the buildroot.
with temporary_dir(root_dir=self.workdir) as comm_dir:
sources_map_path = os.path.join(comm_dir, 'sources_map.json')
mode = 'w' if PY3 else 'wb'
with open(sources_map_path, mode) as fp:
with open(sources_map_path, 'w') as fp:
json.dump(sources_map, fp)

renaming_args = [
Expand Down
1 change: 0 additions & 1 deletion src/python/pants/base/BUILD
Expand Up @@ -47,7 +47,6 @@ python_library(
name = 'build_root',
sources = ['build_root.py'],
dependencies = [
'3rdparty/python:future',
'src/python/pants/util:meta',
],
)
Expand Down
4 changes: 0 additions & 4 deletions src/python/pants/base/build_root.py
Expand Up @@ -4,8 +4,6 @@
import os
from contextlib import contextmanager

from future.utils import PY2

from pants.util.meta import Singleton


Expand Down Expand Up @@ -52,8 +50,6 @@ def path(self):
self._root_dir = override_buildroot
else:
self._root_dir = os.path.realpath(self.find_buildroot())
if PY2:
self._root_dir = self._root_dir.decode('utf-8')
return self._root_dir

@path.setter
Expand Down

0 comments on commit 7dea441

Please sign in to comment.