From 6848d0426f138c12bd7d371792fbace37991202d Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Fri, 27 Dec 2019 18:34:53 +0800 Subject: [PATCH] Clean up Python 2 compatibility code. (#5161) --- python-package/xgboost/__init__.py | 2 -- python-package/xgboost/callback.py | 1 - python-package/xgboost/compat.py | 27 +++++---------------- python-package/xgboost/core.py | 29 +++++++---------------- python-package/xgboost/rabit.py | 4 +--- python-package/xgboost/tracker.py | 4 +--- python-package/xgboost/training.py | 2 -- tests/python/regression_test_utilities.py | 2 -- tests/python/test_linear.py | 2 -- 9 files changed, 16 insertions(+), 57 deletions(-) diff --git a/python-package/xgboost/__init__.py b/python-package/xgboost/__init__.py index 24ca59bb1..54a25195d 100644 --- a/python-package/xgboost/__init__.py +++ b/python-package/xgboost/__init__.py @@ -4,8 +4,6 @@ Contributors: https://github.com/dmlc/xgboost/blob/master/CONTRIBUTORS.md """ -from __future__ import absolute_import - import os from .core import DMatrix, Booster diff --git a/python-package/xgboost/callback.py b/python-package/xgboost/callback.py index 5e1dd69b5..b4264cd82 100644 --- a/python-package/xgboost/callback.py +++ b/python-package/xgboost/callback.py @@ -1,7 +1,6 @@ # coding: utf-8 # pylint: disable=invalid-name, too-many-statements """Training Library containing training routines.""" -from __future__ import absolute_import from . import rabit from .core import EarlyStopException diff --git a/python-package/xgboost/compat.py b/python-package/xgboost/compat.py index b68a8f92c..582b905f3 100644 --- a/python-package/xgboost/compat.py +++ b/python-package/xgboost/compat.py @@ -1,30 +1,21 @@ # coding: utf-8 # pylint: disable= invalid-name, unused-import """For compatibility and optional dependencies.""" - -from __future__ import absolute_import - import abc import os import sys from pathlib import PurePath -PY3 = (sys.version_info[0] == 3) +assert (sys.version_info[0] == 3), 'Python 2 is no longer supported.' -if PY3: - # pylint: disable=invalid-name, redefined-builtin - STRING_TYPES = (str,) +# pylint: disable=invalid-name, redefined-builtin +STRING_TYPES = (str,) - def py_str(x): - """convert c string back to python string""" - return x.decode('utf-8') -else: - STRING_TYPES = (basestring,) # pylint: disable=undefined-variable - def py_str(x): - """convert c string back to python string""" - return x +def py_str(x): + """convert c string back to python string""" + return x.decode('utf-8') ############################################################################### @@ -87,12 +78,6 @@ else: # END NUMPY PATHLIB ATTRIBUTION ############################################################################### -# pickle -try: - import cPickle as pickle # noqa -except ImportError: - import pickle # noqa - # pandas try: from pandas import DataFrame, Series diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index 99d09c333..5d4696886 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -18,7 +18,7 @@ import numpy as np import scipy.sparse from .compat import ( - STRING_TYPES, PY3, DataFrame, MultiIndex, py_str, + STRING_TYPES, DataFrame, MultiIndex, py_str, PANDAS_INSTALLED, DataTable, CUDF_INSTALLED, CUDF_DataFrame, CUDF_Series, CUDF_MultiIndex, os_fspath, os_PathLike) @@ -70,11 +70,7 @@ def from_pystr_to_cstr(data): if not isinstance(data, list): raise NotImplementedError pointers = (ctypes.c_char_p * len(data))() - if PY3: - data = [bytes(d, 'utf-8') for d in data] - else: - data = [d.encode('utf-8') if isinstance(d, unicode) else d # pylint: disable=undefined-variable - for d in data] + data = [bytes(d, 'utf-8') for d in data] pointers[:] = data return pointers @@ -89,21 +85,12 @@ def from_cstr_to_pystr(data, length): length : ctypes pointer pointer to length of data """ - if PY3: - res = [] - for i in range(length.value): - try: - res.append(str(data[i].decode('ascii'))) - except UnicodeDecodeError: - res.append(str(data[i].decode('utf-8'))) - else: - res = [] - for i in range(length.value): - try: - res.append(str(data[i].decode('ascii'))) - except UnicodeDecodeError: - # pylint: disable=undefined-variable - res.append(unicode(data[i].decode('utf-8'))) + res = [] + for i in range(length.value): + try: + res.append(str(data[i].decode('ascii'))) + except UnicodeDecodeError: + res.append(str(data[i].decode('utf-8'))) return res diff --git a/python-package/xgboost/rabit.py b/python-package/xgboost/rabit.py index 879756c38..9b39279cc 100644 --- a/python-package/xgboost/rabit.py +++ b/python-package/xgboost/rabit.py @@ -1,14 +1,12 @@ # coding: utf-8 # pylint: disable= invalid-name - """Distributed XGBoost Rabit related API.""" -from __future__ import absolute_import import sys import ctypes +import pickle import numpy as np from .core import _LIB, c_str, STRING_TYPES -from .compat import pickle def _init_rabit(): diff --git a/python-package/xgboost/tracker.py b/python-package/xgboost/tracker.py index 17d266df7..8debf0565 100644 --- a/python-package/xgboost/tracker.py +++ b/python-package/xgboost/tracker.py @@ -5,8 +5,6 @@ which is a specialized version for xgboost tasks. # pylint: disable=invalid-name, missing-docstring, too-many-arguments, too-many-locals # pylint: disable=too-many-branches, too-many-statements, too-many-instance-attributes -from __future__ import absolute_import - import socket import struct import time @@ -336,4 +334,4 @@ class RabitTracker(object): self.thread.join(100) def alive(self): - return self.thread.isAlive() + return self.thread.is_alive() diff --git a/python-package/xgboost/training.py b/python-package/xgboost/training.py index de3761968..142b98e71 100644 --- a/python-package/xgboost/training.py +++ b/python-package/xgboost/training.py @@ -2,8 +2,6 @@ # pylint: disable=too-many-locals, too-many-arguments, invalid-name # pylint: disable=too-many-branches, too-many-statements """Training Library containing training routines.""" -from __future__ import absolute_import - import numpy as np from .core import Booster, STRING_TYPES, XGBoostError, CallbackEnv, EarlyStopException from .compat import (SKLEARN_INSTALLED, XGBStratifiedKFold) diff --git a/tests/python/regression_test_utilities.py b/tests/python/regression_test_utilities.py index 46c240b2f..cf1ddded3 100644 --- a/tests/python/regression_test_utilities.py +++ b/tests/python/regression_test_utilities.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import glob import itertools as it import numpy as np diff --git a/tests/python/test_linear.py b/tests/python/test_linear.py index f429abe9e..31494a56c 100644 --- a/tests/python/test_linear.py +++ b/tests/python/test_linear.py @@ -1,5 +1,3 @@ -from __future__ import print_function - import numpy as np import testing as tm import unittest