Skip to content

Commit

Permalink
Remove some Python 2 compatibility code. (#6624)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertnishihara committed Jan 1, 2020
1 parent ecddaaf commit 480206e
Show file tree
Hide file tree
Showing 27 changed files with 47 additions and 191 deletions.
17 changes: 7 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ dist: xenial
matrix:
include:
- os: linux
env: BAZEL_PYTHON_VERSION=PY3 PYTHON=3.5 PYTHONWARNINGS=ignore
env: PYTHON=3.5 PYTHONWARNINGS=ignore

- os: osx
osx_image: xcode7
env: BAZEL_PYTHON_VERSION=PY3 PYTHON=3.5 PYTHONWARNINGS=ignore
env: PYTHON=3.5 PYTHONWARNINGS=ignore

- os: linux
env:
Expand All @@ -32,7 +32,6 @@ matrix:
- TESTSUITE=streaming
- JDK='Oracle JDK 8'
- RAY_INSTALL_JAVA=1
- BAZEL_PYTHON_VERSION=PY3
- PYTHON=3.5 PYTHONWARNINGS=ignore
install:
- python $TRAVIS_BUILD_DIR/ci/travis/determine_tests_to_run.py
Expand Down Expand Up @@ -143,16 +142,14 @@ script:
- ./ci/suppress_output bazel test --build_tests_only --show_progress_rate_limit=100 --test_output=errors //:all

# ray serve tests
- if [ $RAY_CI_SERVE_AFFECTED == "1" ]; then python -c 'import sys;exit(sys.version_info>=(3,5))' || python -m pytest -v --durations=5 --timeout=300 python/ray/experimental/serve/tests; fi
- if [ $RAY_CI_SERVE_AFFECTED == "1" ]; then python -c 'import sys;exit(sys.version_info>=(3,5))' || ./ci/suppress_output python python/ray/experimental/serve/examples/echo_full.py; fi
- if [ $RAY_CI_SERVE_AFFECTED == "1" ]; then python -m pytest -v --durations=5 --timeout=300 python/ray/experimental/serve/tests; fi
- if [ $RAY_CI_SERVE_AFFECTED == "1" ]; then ./ci/suppress_output python python/ray/experimental/serve/examples/echo_full.py; fi

# ray tests
# Python3.5+ only. Otherwise we will get `SyntaxError` regardless of how we set the tester.
- if [ $RAY_CI_PYTHON_AFFECTED == "1" ]; then python -c 'import sys;exit(sys.version_info>=(3,5))' || RAY_FORCE_DIRECT=0 python -m pytest -v --durations=5 --timeout=300 python/ray/experimental/test/async_test.py; fi
- if [ $RAY_CI_PYTHON_AFFECTED == "1" ]; then python -c 'import sys;exit(sys.version_info>=(3,5))' || RAY_FORCE_DIRECT=0 python -m pytest -v --durations=5 --timeout=300 python/ray/tests/py3_test.py; fi
- if [ $RAY_CI_PYTHON_AFFECTED == "1" ]; then RAY_FORCE_DIRECT=0 python -m pytest -v --durations=5 --timeout=300 python/ray/experimental/test/async_test.py; fi
- if [ $RAY_CI_PYTHON_AFFECTED == "1" ]; then RAY_FORCE_DIRECT=0 python -m pytest -v --durations=5 --timeout=300 python/ray/tests/py3_test.py; fi

# py bazel tests, run using local strategy since PY2 breaks with sandbox
- ./ci/keep_alive bazel test --spawn_strategy=local --flaky_test_attempts=3 --python_version=$BAZEL_PYTHON_VERSION --nocache_test_results --test_verbose_timeout_warnings --incompatible_allow_python_version_transitions=false --incompatible_py3_is_default=false --progress_report_interval=100 --show_progress_rate_limit=100 --show_timestamps --test_output=errors --test_tag_filters=-jenkins_only python/ray/...
- ./ci/keep_alive bazel test --spawn_strategy=local --flaky_test_attempts=3 --nocache_test_results --test_verbose_timeout_warnings --progress_report_interval=100 --show_progress_rate_limit=100 --show_timestamps --test_output=errors --test_tag_filters=-jenkins_only python/ray/...

deploy:
- provider: s3
Expand Down
5 changes: 1 addition & 4 deletions python/ray/experimental/serve/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import sys
from ray.experimental.serve.backend_config import BackendConfig
from ray.experimental.serve.policy import RoutePolicy
if sys.version_info < (3, 0):
raise ImportError("serve is Python 3 only.")

from ray.experimental.serve.api import (
init, create_backend, create_endpoint, link, split, get_handle, stat,
set_backend_config, get_backend_config, accept_batch) # noqa: E402

__all__ = [
"init", "create_backend", "create_endpoint", "link", "split", "get_handle",
"stat", "set_backend_config", "get_backend_config", "BackendConfig",
Expand Down
7 changes: 1 addition & 6 deletions python/ray/experimental/sgd/tests/test_pytorch_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
from __future__ import print_function

import numpy as np
import sys
import torch
import torch.nn as nn
import unittest
from unittest.mock import MagicMock

from ray.experimental.sgd.pytorch.pytorch_runner import PyTorchRunner

if sys.version_info >= (3, 3):
from unittest.mock import MagicMock
else:
from mock import MagicMock


class LinearDataset(torch.utils.data.Dataset):
"""y = a * x + b"""
Expand Down
17 changes: 7 additions & 10 deletions python/ray/function_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,14 @@ def compute_collision_identifier(self, function_or_class):
unnecessarily or fail to give warnings, but the application's
behavior won't change.
"""
if sys.version_info[0] >= 3:
import io
string_file = io.StringIO()
if sys.version_info[1] >= 7:
dis.dis(function_or_class, file=string_file, depth=2)
else:
dis.dis(function_or_class, file=string_file)
collision_identifier = (
function_or_class.__name__ + ":" + string_file.getvalue())
import io
string_file = io.StringIO()
if sys.version_info[1] >= 7:
dis.dis(function_or_class, file=string_file, depth=2)
else:
collision_identifier = function_or_class.__name__
dis.dis(function_or_class, file=string_file)
collision_identifier = (
function_or_class.__name__ + ":" + string_file.getvalue())

# Return a hash of the identifier in case it is too large.
return hashlib.sha1(collision_identifier.encode("ascii")).digest()
Expand Down
20 changes: 7 additions & 13 deletions python/ray/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
# using logging.basicConfig in its entry/init points.
logger = logging.getLogger(__name__)

PY3 = sys.version_info.major >= 3
SESSION_LATEST = "session_latest"


Expand Down Expand Up @@ -599,12 +598,10 @@ def start_head_processes(self):
self.start_redis()
self.start_monitor()
self.start_raylet_monitor()
# The dashboard is Python3.x only.
if PY3:
if self._ray_params.include_webui:
self.start_dashboard(require_webui=True)
elif self._ray_params.include_webui is None:
self.start_dashboard(require_webui=False)
if self._ray_params.include_webui:
self.start_dashboard(require_webui=True)
elif self._ray_params.include_webui is None:
self.start_dashboard(require_webui=False)

def start_ray_processes(self):
"""Start all of the processes on the node."""
Expand All @@ -614,8 +611,7 @@ def start_ray_processes(self):

self.start_plasma_store()
self.start_raylet()
if PY3:
self.start_reporter()
self.start_reporter()

if self._ray_params.include_log_monitor:
self.start_log_monitor()
Expand Down Expand Up @@ -755,10 +751,8 @@ def kill_reporter(self, check_alive=True):
check_alive (bool): Raise an exception if the process was already
dead.
"""
# reporter is started only in PY3.
if PY3:
self._kill_process_type(
ray_constants.PROCESS_TYPE_REPORTER, check_alive=check_alive)
self._kill_process_type(
ray_constants.PROCESS_TYPE_REPORTER, check_alive=check_alive)

def kill_dashboard(self, check_alive=True):
"""Kill the dashboard.
Expand Down
7 changes: 1 addition & 6 deletions python/ray/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,7 @@ def start_ray_process(command,
# in interactive sessions. This is only supported in Python 3.3 and above.
def block_sigint():
import signal
import sys
if sys.version_info >= (3, 3):
signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGINT})
signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGINT})

process = subprocess.Popen(
command,
Expand Down Expand Up @@ -1068,9 +1066,6 @@ def start_dashboard(require_webui,
if redis_password:
command += ["--redis-password", redis_password]

if sys.version_info <= (3, 0):
return None, None

webui_dependencies_present = True
try:
import aiohttp # noqa: F401
Expand Down
15 changes: 0 additions & 15 deletions python/ray/tests/test_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ def foo(self):
ray.get(actor.foo.remote())


@pytest.mark.skipif(
sys.version_info >= (3, 0), reason="This test requires Python 2.")
def test_old_style_error(ray_start_regular):
with pytest.raises(TypeError):

@ray.remote
class Actor:
pass


def test_keyword_args(ray_start_regular):
@ray.remote
class Actor(object):
Expand Down Expand Up @@ -189,8 +179,6 @@ def get_values2(self, f3):
assert results2[2].x == 3


@pytest.mark.skipif(
sys.version_info < (3, 0), reason="This test requires Python 3.")
def test_actor_class_attributes(ray_start_regular):
class Grandparent(object):
GRANDPARENT = 2
Expand Down Expand Up @@ -423,8 +411,6 @@ def getpid(self):
[ray.test_utils.wait_for_pid_to_exit(pid) for pid in pids]


@pytest.mark.skipif(
sys.version_info < (3, 0), reason="This test requires Python 3.")
def test_actor_method_deletion(ray_start_regular):
@ray.remote
class Actor(object):
Expand Down Expand Up @@ -1497,5 +1483,4 @@ def set_count(self, count):

if __name__ == "__main__":
import pytest
import sys
sys.exit(pytest.main(["-v", __file__]))
6 changes: 0 additions & 6 deletions python/ray/tests/test_actor_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,6 @@ def get_location_and_ids(self):
assert ready_ids == []


@pytest.mark.skipif(
sys.version_info < (3, 0), reason="This test requires Python 3.")
def test_actors_and_tasks_with_gpus(ray_start_cluster):
cluster = ray_start_cluster
num_nodes = 3
Expand Down Expand Up @@ -638,9 +636,6 @@ def blocking_method(self):
assert remaining_ids == [x_id]


@pytest.mark.skipif(
sys.version_info < (3, 0),
reason="This test is currently failing on Python 2.7.")
def test_lifetime_and_transient_resources(ray_start_regular):
# This actor acquires resources only when running methods.
@ray.remote
Expand Down Expand Up @@ -743,5 +738,4 @@ def method(self):

if __name__ == "__main__":
import pytest
import sys
sys.exit(pytest.main(["-v", __file__]))
11 changes: 3 additions & 8 deletions python/ray/tests/test_advanced_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,9 @@ class CaptureOutputAndError(object):
"""

def __init__(self, captured_output_and_error):
if sys.version_info >= (3, 0):
import io
self.output_buffer = io.StringIO()
self.error_buffer = io.StringIO()
else:
import cStringIO
self.output_buffer = cStringIO.StringIO()
self.error_buffer = cStringIO.StringIO()
import io
self.output_buffer = io.StringIO()
self.error_buffer = io.StringIO()
self.captured_output_and_error = captured_output_and_error

def __enter__(self):
Expand Down
5 changes: 1 addition & 4 deletions python/ray/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import division
from __future__ import print_function

from importlib import reload
import numpy as np
from numpy.testing import assert_equal, assert_almost_equal
import pytest
Expand All @@ -12,9 +13,6 @@
import ray.experimental.array.distributed as da
import ray.cluster_utils

if sys.version_info >= (3, 0):
from importlib import reload


@pytest.fixture
def reload_modules():
Expand Down Expand Up @@ -220,5 +218,4 @@ def test_dist_qr(d1, d2):

if __name__ == "__main__":
import pytest
import sys
sys.exit(pytest.main(["-v", __file__]))
22 changes: 1 addition & 21 deletions python/ray/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ def test_simple_serialization(ray_start_regular):
np.float64(1.9),
]

if sys.version_info < (3, 0):
primitive_objects.append(long(0)) # noqa: E501,F821

composite_objects = (
[[obj]
for obj in primitive_objects] + [(obj, )
Expand Down Expand Up @@ -185,17 +182,7 @@ def assert_equal(obj1, obj2):
assert obj1 == obj2, "Objects {} and {} are different.".format(
obj1, obj2)

if sys.version_info >= (3, 0):
long_extras = [0, np.array([["hi", u"hi"], [1.3, 1]])]
else:

long_extras = [
long(0), # noqa: E501,F821
np.array([
["hi", u"hi"],
[1.3, long(1)] # noqa: E501,F821
])
]
long_extras = [0, np.array([["hi", u"hi"], [1.3, 1]])]

PRIMITIVE_OBJECTS = [
0, 0.0, 0.9, 1 << 62, 1 << 100, 1 << 999, [1 << 100, [1 << 100]], "a",
Expand Down Expand Up @@ -810,8 +797,6 @@ def f3(x):
assert ray.get(f3.remote(4)) == 4


@pytest.mark.skipif(
sys.version_info < (3, 0), reason="This test requires Python 3.")
@pytest.mark.parametrize(
"ray_start_regular", [{
"local_mode": True
Expand Down Expand Up @@ -847,8 +832,6 @@ def test_function(fn, remote_fn):
ray.get(remote_test_function.remote(local_method, actor_method))


@pytest.mark.skipif(
sys.version_info < (3, 0), reason="This test requires Python 3.")
@pytest.mark.parametrize(
"ray_start_regular", [{
"local_mode": True
Expand Down Expand Up @@ -890,8 +873,6 @@ def test_function(fn, remote_fn):
ray.get(remote_test_function.remote(local_method, actor_method))


@pytest.mark.skipif(
sys.version_info < (3, 0), reason="This test requires Python 3.")
@pytest.mark.parametrize(
"ray_start_regular", [{
"local_mode": True
Expand Down Expand Up @@ -1655,5 +1636,4 @@ def f(delay):

if __name__ == "__main__":
import pytest
import sys
sys.exit(pytest.main(["-v", __file__]))
1 change: 0 additions & 1 deletion python/ray/tests/test_debug_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def f():

if __name__ == "__main__":
import pytest
import sys
# Make subprocess happy in bazel.
os.environ["LC_ALL"] = "en_US.UTF-8"
os.environ["LANG"] = "en_US.UTF-8"
Expand Down
2 changes: 0 additions & 2 deletions python/ray/tests/test_failure.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,6 @@ def g():
wait_for_errors(ray_constants.WORKER_POOL_LARGE_ERROR, 1)


@pytest.mark.skipif(
sys.version_info < (3, 0), reason="This test requires Python 3.")
def test_warning_for_many_duplicate_remote_functions_and_actors(shutdown_only):
ray.init(num_cpus=1)

Expand Down
7 changes: 1 addition & 6 deletions python/ray/tests/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@
import yaml
from click.testing import CliRunner
import sys
from unittest.mock import patch, DEFAULT

from contextlib import contextmanager

from ray.projects.scripts import session_start, session_execute
import ray

if sys.version_info >= (3, 3):
from unittest.mock import patch, DEFAULT
else:
from mock import patch, DEFAULT

TEST_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "project_files")

Expand Down Expand Up @@ -240,7 +236,6 @@ def test_session_create_multiple():


if __name__ == "__main__":
import sys
# Make subprocess happy in bazel.
os.environ["LC_ALL"] = "en_US.UTF-8"
os.environ["LANG"] = "en_US.UTF-8"
Expand Down
Loading

0 comments on commit 480206e

Please sign in to comment.