diff --git a/cpplint.py b/cpplint.py index 04fd675767..62662bf5ad 100755 --- a/cpplint.py +++ b/cpplint.py @@ -3949,9 +3949,8 @@ def CheckBraces(filename, clean_lines, linenum, error): # following line if it is part of an array initialization and would not fit # within the 80 character limit of the preceding line. prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] - if (not Search(r'[,;:}{(]\s*$', prevline) and - not Match(r'\s*#', prevline) and - not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)): + if (not Search(r'[,;:}{(]\s*$', prevline) and not Match(r'\s*#', prevline) + and not (len(prevline) > _line_length - 2 and '[]' in prevline)): error(filename, linenum, 'whitespace/braces', 4, '{ should almost always be at the end of the previous line') @@ -4467,28 +4466,6 @@ def CheckAltTokens(filename, clean_lines, linenum, error): _ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1))) -def GetLineWidth(line): - """Determines the width of the line in column positions. - - Args: - line: A string, which may be a Unicode string. - - Returns: - The width of the line in column positions, accounting for Unicode - combining characters and wide characters. - """ - if sys.version_info == 2 and isinstance(line, unicode): - width = 0 - for uc in unicodedata.normalize('NFC', line): - if unicodedata.east_asian_width(uc) in ('W', 'F'): - width += 2 - elif not unicodedata.combining(uc): - width += 1 - return width - else: - return len(line) - - def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, error): """Checks rules from the 'C++ style rules' section of cppguide.html. @@ -4574,8 +4551,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state, not Match(r'^\s*//.*http(s?)://\S*$', line) and not Match(r'^\s*//\s*[^\s]*$', line) and not Match(r'^// \$Id:.*#[0-9]+ \$$', line)): - line_width = GetLineWidth(line) - if line_width > _line_length: + if len(line) > _line_length: error(filename, linenum, 'whitespace/line_length', 2, 'Lines should be <= %i characters long' % _line_length) diff --git a/fetch.py b/fetch.py index 39268f3580..80f8927d0f 100755 --- a/fetch.py +++ b/fetch.py @@ -115,8 +115,7 @@ class GclientGitCheckout(GclientCheckout, GitCheckout): def _format_spec(self): def _format_literal(lit): - if isinstance(lit, str) or (sys.version_info.major == 2 and - isinstance(lit, unicode)): + if isinstance(lit, str): return '"%s"' % lit if isinstance(lit, list): return '[%s]' % ', '.join(_format_literal(i) for i in lit) diff --git a/fix_encoding.py b/fix_encoding.py index b14494f398..e97ea05203 100644 --- a/fix_encoding.py +++ b/fix_encoding.py @@ -14,10 +14,6 @@ import os import sys -# Prevents initializing multiple times. -_SYS_ARGV_PROCESSED = False - - def complain(message): """If any exception occurs in this file, we'll probably try to print it on stderr, which makes for frustrating debugging if stderr is directed @@ -72,63 +68,6 @@ def fix_default_encoding(): ############################### # Windows specific - -def fix_win_sys_argv(encoding): - """Converts sys.argv to 'encoding' encoded string. - - utf-8 is recommended. - - Works around . - """ - global _SYS_ARGV_PROCESSED - if _SYS_ARGV_PROCESSED: - return False - - if sys.version_info.major == 3: - _SYS_ARGV_PROCESSED = True - return True - - # These types are available on linux but not Mac. - # pylint: disable=no-name-in-module,F0401 - from ctypes import byref, c_int, POINTER, windll, WINFUNCTYPE - from ctypes.wintypes import LPCWSTR, LPWSTR - - # - GetCommandLineW = WINFUNCTYPE(LPWSTR)(('GetCommandLineW', windll.kernel32)) - # - CommandLineToArgvW = WINFUNCTYPE(POINTER(LPWSTR), LPCWSTR, POINTER(c_int))( - ('CommandLineToArgvW', windll.shell32)) - - argc = c_int(0) - argv_unicode = CommandLineToArgvW(GetCommandLineW(), byref(argc)) - argv = [ - argv_unicode[i].encode(encoding, 'replace') for i in range(0, argc.value) - ] - - if not hasattr(sys, 'frozen'): - # If this is an executable produced by py2exe or bbfreeze, then it - # will have been invoked directly. Otherwise, unicode_argv[0] is the - # Python interpreter, so skip that. - argv = argv[1:] - - # Also skip option arguments to the Python interpreter. - while len(argv) > 0: - arg = argv[0] - if not arg.startswith(b'-') or arg == b'-': - break - argv = argv[1:] - if arg == u'-m': - # sys.argv[0] should really be the absolute path of the - # module source, but never mind. - break - if arg == u'-c': - argv[0] = u'-c' - break - sys.argv = argv - _SYS_ARGV_PROCESSED = True - return True - - def fix_win_codec(): """Works around .""" # @@ -216,10 +155,7 @@ class WinUnicodeConsoleOutput(WinUnicodeOutputBase): def write(self, text): try: - if sys.version_info.major == 2 and not isinstance(text, unicode): - # Convert to unicode. - text = str(text).decode(self.encoding, 'replace') - elif sys.version_info.major == 3 and isinstance(text, bytes): + if isinstance(text, bytes): # Bytestrings need to be decoded to a string before being passed to # Windows. text = text.decode(self.encoding, 'replace') @@ -270,10 +206,7 @@ class WinUnicodeOutput(WinUnicodeOutputBase): def write(self, text): try: - if sys.version_info.major == 2 and isinstance(text, unicode): - # Replace characters that cannot be printed instead of failing. - text = text.encode(self.encoding, 'replace') - if sys.version_info.major == 3 and isinstance(text, bytes): + if isinstance(text, bytes): # Replace characters that cannot be printed instead of failing. text = text.decode(self.encoding, 'replace') # When redirecting to a file or process any \n characters will be replaced @@ -385,6 +318,5 @@ def fix_encoding(): if sys.platform == 'win32': encoding = sys.getdefaultencoding() - ret &= fix_win_sys_argv(encoding) ret &= fix_win_console(encoding) return ret diff --git a/recipes/recipe_modules/bot_update/resources/bot_update.py b/recipes/recipe_modules/bot_update/resources/bot_update.py index 057bab7078..34b47b5090 100755 --- a/recipes/recipe_modules/bot_update/resources/bot_update.py +++ b/recipes/recipe_modules/bot_update/resources/bot_update.py @@ -533,11 +533,9 @@ def get_total_disk_space(): if sys.platform.startswith('win'): _, total, free = (ctypes.c_ulonglong(), ctypes.c_ulonglong(), \ ctypes.c_ulonglong()) - if sys.version_info >= (3,) or isinstance(cwd, unicode): - fn = ctypes.windll.kernel32.GetDiskFreeSpaceExW - else: - fn = ctypes.windll.kernel32.GetDiskFreeSpaceExA - ret = fn(cwd, ctypes.byref(_), ctypes.byref(total), ctypes.byref(free)) + ret = ctypes.windll.kernel32.GetDiskFreeSpaceExW(cwd, ctypes.byref(_), + ctypes.byref(total), + ctypes.byref(free)) if ret == 0: # WinError() will fetch the last error code. raise ctypes.WinError() diff --git a/subprocess2.py b/subprocess2.py index 2e97e35716..481917a383 100644 --- a/subprocess2.py +++ b/subprocess2.py @@ -15,21 +15,11 @@ import subprocess import sys import threading -# Cache the string-escape codec to ensure subprocess can find it later. -# See crbug.com/912292#c2 for context. -if sys.version_info.major == 2: - codecs.lookup('string-escape') - # Sends stdout or stderr to os.devnull. - DEVNULL = open(os.devnull, 'r+') -else: - # pylint: disable=redefined-builtin - basestring = (str, bytes) - DEVNULL = subprocess.DEVNULL - # Constants forwarded from subprocess. PIPE = subprocess.PIPE STDOUT = subprocess.STDOUT +DEVNULL = subprocess.DEVNULL class CalledProcessError(subprocess.CalledProcessError): @@ -122,7 +112,7 @@ class Popen(subprocess.Popen): env = get_english_env(kwargs.get('env')) if env: kwargs['env'] = env - if kwargs.get('env') is not None and sys.version_info.major != 2: + if kwargs.get('env') is not None: # Subprocess expects environment variables to be strings in Python 3. def ensure_str(value): if isinstance(value, bytes): @@ -140,7 +130,7 @@ class Popen(subprocess.Popen): # the list. kwargs['shell'] = bool(sys.platform=='win32') - if isinstance(args, basestring): + if isinstance(args, (str, bytes)): tmp_str = args elif isinstance(args, (list, tuple)): tmp_str = ' '.join(args) @@ -183,7 +173,7 @@ def communicate(args, **kwargs): stdin = None # When stdin is passed as an argument, use it as the actual input data and # set the Popen() parameter accordingly. - if 'stdin' in kwargs and isinstance(kwargs['stdin'], basestring): + if 'stdin' in kwargs and isinstance(kwargs['stdin'], (str, bytes)): stdin = kwargs['stdin'] kwargs['stdin'] = PIPE @@ -245,7 +235,6 @@ def check_output(args, **kwargs): Captures stdout of a process call and returns stdout only. - Throws if return code is not 0. - - Works even prior to python 2.7. - Blocks stdin by default if not specified since no output will be visible. - As per doc, "The stdout argument is not allowed as it is used internally." """ diff --git a/testing_support/filesystem_mock.py b/testing_support/filesystem_mock.py index 47c86e32ab..563447c601 100644 --- a/testing_support/filesystem_mock.py +++ b/testing_support/filesystem_mock.py @@ -6,12 +6,7 @@ import errno import fnmatch import os import re -import sys - -if sys.version_info.major == 2: - from StringIO import StringIO -else: - from io import StringIO +from io import StringIO def _RaiseNotFound(path): diff --git a/testing_support/subprocess2_test_script.py b/testing_support/subprocess2_test_script.py index f55e198af5..13bf1286bb 100644 --- a/testing_support/subprocess2_test_script.py +++ b/testing_support/subprocess2_test_script.py @@ -38,19 +38,11 @@ if args: def do(string): if options.stdout: - if sys.version_info.major == 2: - sys.stdout.write(string.upper()) - sys.stdout.write(options.eol) - else: - sys.stdout.buffer.write(string.upper().encode('utf-8')) - sys.stdout.buffer.write(options.eol.encode('utf-8')) + sys.stdout.buffer.write(string.upper().encode('utf-8')) + sys.stdout.buffer.write(options.eol.encode('utf-8')) if options.stderr: - if sys.version_info.major == 2: - sys.stderr.write(string.lower()) - sys.stderr.write(options.eol) - else: - sys.stderr.buffer.write(string.lower().encode('utf-8')) - sys.stderr.buffer.write(options.eol.encode('utf-8')) + sys.stderr.buffer.write(string.lower().encode('utf-8')) + sys.stderr.buffer.write(options.eol.encode('utf-8')) sys.stderr.flush() diff --git a/tests/auth_test.py b/tests/auth_test.py index e8f07c35fa..46ab8ef383 100755 --- a/tests/auth_test.py +++ b/tests/auth_test.py @@ -11,13 +11,7 @@ import json import os import unittest import sys - -if sys.version_info.major == 2: - import mock - BUILTIN_OPEN = '__builtin__.open' -else: - from unittest import mock - BUILTIN_OPEN = 'builtins.open' +from unittest import mock sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) @@ -121,7 +115,7 @@ class AccessTokenTest(unittest.TestCase): class HasLuciContextLocalAuthTest(unittest.TestCase): def setUp(self): mock.patch('os.environ').start() - mock.patch(BUILTIN_OPEN, mock.mock_open()).start() + mock.patch('builtins.open', mock.mock_open()).start() self.addCleanup(mock.patch.stopall) def testNoLuciContextEnvVar(self): diff --git a/tests/detect_host_arch_test.py b/tests/detect_host_arch_test.py index 5bba516475..39b5e241d8 100644 --- a/tests/detect_host_arch_test.py +++ b/tests/detect_host_arch_test.py @@ -7,11 +7,7 @@ import os import platform import sys import unittest - -if sys.version_info.major == 2: - import mock -else: - from unittest import mock +from unittest import mock sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) diff --git a/tests/fetch_test.py b/tests/fetch_test.py index 4848633759..56ac80dd64 100755 --- a/tests/fetch_test.py +++ b/tests/fetch_test.py @@ -13,15 +13,8 @@ import subprocess import sys import tempfile import unittest - -import distutils - -if sys.version_info.major == 2: - from StringIO import StringIO - import mock -else: - from io import StringIO - from unittest import mock +from io import StringIO +from unittest import mock sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) diff --git a/tests/gclient_utils_test.py b/tests/gclient_utils_test.py index e31ed04a9f..2234698c27 100755 --- a/tests/gclient_utils_test.py +++ b/tests/gclient_utils_test.py @@ -39,14 +39,10 @@ class CheckCallAndFilterTestCase(unittest.TestCase): self.printfn = io.StringIO() self.stdout = io.BytesIO() self.kids = [] - if sys.version_info.major == 2: - mock.patch('sys.stdout', self.stdout).start() - mock.patch('__builtin__.print', self.printfn.write).start() - else: - mock.patch('sys.stdout', mock.Mock()).start() - mock.patch('sys.stdout.buffer', self.stdout).start() - mock.patch('sys.stdout.isatty', return_value=False).start() - mock.patch('builtins.print', self.printfn.write).start() + mock.patch('sys.stdout', mock.Mock()).start() + mock.patch('sys.stdout.buffer', self.stdout).start() + mock.patch('sys.stdout.isatty', return_value=False).start() + mock.patch('builtins.print', self.printfn.write).start() mock.patch('sys.stdout.flush', lambda: None).start() self.addCleanup(mock.patch.stopall) diff --git a/tests/lockfile_test.py b/tests/lockfile_test.py index a29540ece4..60aaa4ec1e 100755 --- a/tests/lockfile_test.py +++ b/tests/lockfile_test.py @@ -11,13 +11,8 @@ import sys import tempfile import threading import unittest - -if sys.version_info.major == 2: - import mock - import Queue -else: - from unittest import mock - import queue as Queue +from unittest import mock +import queue DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, DEPOT_TOOLS_ROOT) @@ -48,9 +43,9 @@ class LockTest(unittest.TestCase): to acquire the same file lock with timeout.''' # Queues q_f1 and q_sleep are used to controll execution of individual # threads. - q_f1 = Queue.Queue() - q_sleep = Queue.Queue() - results = Queue.Queue() + q_f1 = queue.Queue() + q_sleep = queue.Queue() + results = queue.Queue() def side_effect(arg): '''side_effect is called when with l.lock is blocked. In this unit test diff --git a/tests/rdb_wrapper_test.py b/tests/rdb_wrapper_test.py index 1ae896c1b9..4137e6149e 100755 --- a/tests/rdb_wrapper_test.py +++ b/tests/rdb_wrapper_test.py @@ -11,16 +11,10 @@ import contextlib import json import logging import os -import requests import sys import tempfile -import time import unittest - -if sys.version_info.major == 2: - import mock -else: - from unittest import mock +from unittest import mock sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) diff --git a/tests/scm_unittest.py b/tests/scm_unittest.py index 661a9f3d04..a4cfbe5cb8 100755 --- a/tests/scm_unittest.py +++ b/tests/scm_unittest.py @@ -9,11 +9,7 @@ import logging import os import sys import unittest - -if sys.version_info.major == 2: - import mock -else: - from unittest import mock +from unittest import mock sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) diff --git a/tests/subcommand_test.py b/tests/subcommand_test.py index 1b8315a991..600a7e8fa8 100755 --- a/tests/subcommand_test.py +++ b/tests/subcommand_test.py @@ -6,11 +6,7 @@ import os import sys import unittest - -if sys.version_info.major == 2: - import mock -else: - from unittest import mock +from unittest import mock _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, _ROOT) diff --git a/tests/subprocess2_test.py b/tests/subprocess2_test.py index e7733ef50d..489bac9052 100755 --- a/tests/subprocess2_test.py +++ b/tests/subprocess2_test.py @@ -8,11 +8,7 @@ import os import sys import unittest - -if sys.version_info.major == 2: - import mock -else: - from unittest import mock +from unittest import mock DEPOT_TOOLS = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, DEPOT_TOOLS) @@ -84,11 +80,8 @@ class DefaultsTest(unittest.TestCase): @mock.patch('subprocess.Popen.__init__') def test_env_type(self, mockPopen): - if sys.version_info.major != 2: - subprocess2.Popen(['foo'], env={b'key': b'value'}) - mockPopen.assert_called_with(['foo'], - env={'key': 'value'}, - shell=mock.ANY) + subprocess2.Popen(['foo'], env={b'key': b'value'}) + mockPopen.assert_called_with(['foo'], env={'key': 'value'}, shell=mock.ANY) def _run_test(with_subprocess=True): @@ -130,13 +123,8 @@ class SmokeTests(unittest.TestCase): def _check_exception(self, subp, e, stdout, stderr, returncode): """On exception, look if the exception members are set correctly.""" self.assertEqual(returncode, e.returncode) - if subp is subprocess2 or sys.version_info.major == 3: - self.assertEqual(stdout, e.stdout) - self.assertEqual(stderr, e.stderr) - else: - # subprocess never save the output. - self.assertFalse(hasattr(e, 'stdout')) - self.assertFalse(hasattr(e, 'stderr')) + self.assertEqual(stdout, e.stdout) + self.assertEqual(stderr, e.stderr) def test_check_output_no_stdout(self): for subp in (subprocess, subprocess2): diff --git a/tests/utils_test.py b/tests/utils_test.py index 89620dbeb9..5c505209cd 100755 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -7,11 +7,7 @@ import logging import os import sys import unittest - -if sys.version_info.major == 2: - import mock -else: - from unittest import mock +from unittest import mock DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, DEPOT_TOOLS_ROOT) diff --git a/tests/watchlists_unittest.py b/tests/watchlists_unittest.py index afa731a211..e766d44a05 100755 --- a/tests/watchlists_unittest.py +++ b/tests/watchlists_unittest.py @@ -10,11 +10,7 @@ import os import sys import unittest - -if sys.version_info.major == 2: - import mock -else: - from unittest import mock +from unittest import mock sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) diff --git a/win_toolchain/get_toolchain_if_necessary.py b/win_toolchain/get_toolchain_if_necessary.py index 6b25ad7e7d..e4d606f2f0 100755 --- a/win_toolchain/get_toolchain_if_necessary.py +++ b/win_toolchain/get_toolchain_if_necessary.py @@ -31,13 +31,9 @@ import subprocess import sys import tempfile import time -if sys.version_info[0] < 3: - from urllib2 import urlopen, URLError - from urlparse import urljoin -else: - from urllib.request import urlopen - from urllib.parse import urljoin - from urllib.error import URLError +from urllib.request import urlopen +from urllib.parse import urljoin +from urllib.error import URLError import zipfile # Environment variable that, if set, specifies the default Visual Studio @@ -185,10 +181,7 @@ def CalculateHash(root, expected_hash): if expected_hash: path_without_hash = path_without_hash.replace( os.path.join(root, expected_hash).replace('/', '\\'), root) - if sys.version_info[0] < 3: - digest.update(path_without_hash.lower()) - else: - digest.update(bytes(path_without_hash.lower(), 'utf-8')) + digest.update(bytes(path_without_hash.lower(), 'utf-8')) with open(path, 'rb') as f: digest.update(f.read())