Drop py2 support in various files

python3 is the only supported version of python in depot_tools.
Remove py2 support from files including:
  * cpplint.py
  * subprocess2.py
  * many tests and testing_support files

Bug: 1475402
Change-Id: I67a98188bc13c4dc119e6158a37bd236bfd6ea70
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4824474
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Reviewed-by: Scott Lee <ddoman@chromium.org>
This commit is contained in:
Gavin Mak
2023-09-05 18:02:24 +00:00
committed by LUCI CQ
parent 7e25126240
commit 512f3cb37e
19 changed files with 46 additions and 232 deletions

30
cpplint.py vendored
View File

@@ -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 # following line if it is part of an array initialization and would not fit
# within the 80 character limit of the preceding line. # within the 80 character limit of the preceding line.
prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
if (not Search(r'[,;:}{(]\s*$', prevline) and if (not Search(r'[,;:}{(]\s*$', prevline) and not Match(r'\s*#', prevline)
not Match(r'\s*#', prevline) and and not (len(prevline) > _line_length - 2 and '[]' in prevline)):
not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)):
error(filename, linenum, 'whitespace/braces', 4, error(filename, linenum, 'whitespace/braces', 4,
'{ should almost always be at the end of the previous line') '{ 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))) _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, def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
error): error):
"""Checks rules from the 'C++ style rules' section of cppguide.html. """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*//.*http(s?)://\S*$', line) and
not Match(r'^\s*//\s*[^\s]*$', line) and not Match(r'^\s*//\s*[^\s]*$', line) and
not Match(r'^// \$Id:.*#[0-9]+ \$$', line)): not Match(r'^// \$Id:.*#[0-9]+ \$$', line)):
line_width = GetLineWidth(line) if len(line) > _line_length:
if line_width > _line_length:
error(filename, linenum, 'whitespace/line_length', 2, error(filename, linenum, 'whitespace/line_length', 2,
'Lines should be <= %i characters long' % _line_length) 'Lines should be <= %i characters long' % _line_length)

View File

@@ -115,8 +115,7 @@ class GclientGitCheckout(GclientCheckout, GitCheckout):
def _format_spec(self): def _format_spec(self):
def _format_literal(lit): def _format_literal(lit):
if isinstance(lit, str) or (sys.version_info.major == 2 and if isinstance(lit, str):
isinstance(lit, unicode)):
return '"%s"' % lit return '"%s"' % lit
if isinstance(lit, list): if isinstance(lit, list):
return '[%s]' % ', '.join(_format_literal(i) for i in lit) return '[%s]' % ', '.join(_format_literal(i) for i in lit)

View File

@@ -14,10 +14,6 @@ import os
import sys import sys
# Prevents initializing multiple times.
_SYS_ARGV_PROCESSED = False
def complain(message): def complain(message):
"""If any exception occurs in this file, we'll probably try to print it """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 on stderr, which makes for frustrating debugging if stderr is directed
@@ -72,63 +68,6 @@ def fix_default_encoding():
############################### ###############################
# Windows specific # Windows specific
def fix_win_sys_argv(encoding):
"""Converts sys.argv to 'encoding' encoded string.
utf-8 is recommended.
Works around <http://bugs.python.org/issue2128>.
"""
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
# <http://msdn.microsoft.com/en-us/library/ms683156.aspx>
GetCommandLineW = WINFUNCTYPE(LPWSTR)(('GetCommandLineW', windll.kernel32))
# <http://msdn.microsoft.com/en-us/library/bb776391.aspx>
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(): def fix_win_codec():
"""Works around <http://bugs.python.org/issue6058>.""" """Works around <http://bugs.python.org/issue6058>."""
# <http://msdn.microsoft.com/en-us/library/dd317756.aspx> # <http://msdn.microsoft.com/en-us/library/dd317756.aspx>
@@ -216,10 +155,7 @@ class WinUnicodeConsoleOutput(WinUnicodeOutputBase):
def write(self, text): def write(self, text):
try: try:
if sys.version_info.major == 2 and not isinstance(text, unicode): if isinstance(text, bytes):
# Convert to unicode.
text = str(text).decode(self.encoding, 'replace')
elif sys.version_info.major == 3 and isinstance(text, bytes):
# Bytestrings need to be decoded to a string before being passed to # Bytestrings need to be decoded to a string before being passed to
# Windows. # Windows.
text = text.decode(self.encoding, 'replace') text = text.decode(self.encoding, 'replace')
@@ -270,10 +206,7 @@ class WinUnicodeOutput(WinUnicodeOutputBase):
def write(self, text): def write(self, text):
try: try:
if sys.version_info.major == 2 and isinstance(text, unicode): if isinstance(text, bytes):
# 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):
# Replace characters that cannot be printed instead of failing. # Replace characters that cannot be printed instead of failing.
text = text.decode(self.encoding, 'replace') text = text.decode(self.encoding, 'replace')
# When redirecting to a file or process any \n characters will be replaced # When redirecting to a file or process any \n characters will be replaced
@@ -385,6 +318,5 @@ def fix_encoding():
if sys.platform == 'win32': if sys.platform == 'win32':
encoding = sys.getdefaultencoding() encoding = sys.getdefaultencoding()
ret &= fix_win_sys_argv(encoding)
ret &= fix_win_console(encoding) ret &= fix_win_console(encoding)
return ret return ret

View File

@@ -533,11 +533,9 @@ def get_total_disk_space():
if sys.platform.startswith('win'): if sys.platform.startswith('win'):
_, total, free = (ctypes.c_ulonglong(), ctypes.c_ulonglong(), \ _, total, free = (ctypes.c_ulonglong(), ctypes.c_ulonglong(), \
ctypes.c_ulonglong()) ctypes.c_ulonglong())
if sys.version_info >= (3,) or isinstance(cwd, unicode): ret = ctypes.windll.kernel32.GetDiskFreeSpaceExW(cwd, ctypes.byref(_),
fn = ctypes.windll.kernel32.GetDiskFreeSpaceExW ctypes.byref(total),
else: ctypes.byref(free))
fn = ctypes.windll.kernel32.GetDiskFreeSpaceExA
ret = fn(cwd, ctypes.byref(_), ctypes.byref(total), ctypes.byref(free))
if ret == 0: if ret == 0:
# WinError() will fetch the last error code. # WinError() will fetch the last error code.
raise ctypes.WinError() raise ctypes.WinError()

View File

@@ -15,21 +15,11 @@ import subprocess
import sys import sys
import threading 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. # Constants forwarded from subprocess.
PIPE = subprocess.PIPE PIPE = subprocess.PIPE
STDOUT = subprocess.STDOUT STDOUT = subprocess.STDOUT
DEVNULL = subprocess.DEVNULL
class CalledProcessError(subprocess.CalledProcessError): class CalledProcessError(subprocess.CalledProcessError):
@@ -122,7 +112,7 @@ class Popen(subprocess.Popen):
env = get_english_env(kwargs.get('env')) env = get_english_env(kwargs.get('env'))
if env: if env:
kwargs['env'] = 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. # Subprocess expects environment variables to be strings in Python 3.
def ensure_str(value): def ensure_str(value):
if isinstance(value, bytes): if isinstance(value, bytes):
@@ -140,7 +130,7 @@ class Popen(subprocess.Popen):
# the list. # the list.
kwargs['shell'] = bool(sys.platform=='win32') kwargs['shell'] = bool(sys.platform=='win32')
if isinstance(args, basestring): if isinstance(args, (str, bytes)):
tmp_str = args tmp_str = args
elif isinstance(args, (list, tuple)): elif isinstance(args, (list, tuple)):
tmp_str = ' '.join(args) tmp_str = ' '.join(args)
@@ -183,7 +173,7 @@ def communicate(args, **kwargs):
stdin = None stdin = None
# When stdin is passed as an argument, use it as the actual input data and # When stdin is passed as an argument, use it as the actual input data and
# set the Popen() parameter accordingly. # 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'] stdin = kwargs['stdin']
kwargs['stdin'] = PIPE kwargs['stdin'] = PIPE
@@ -245,7 +235,6 @@ def check_output(args, **kwargs):
Captures stdout of a process call and returns stdout only. Captures stdout of a process call and returns stdout only.
- Throws if return code is not 0. - 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. - 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." - As per doc, "The stdout argument is not allowed as it is used internally."
""" """

View File

@@ -6,12 +6,7 @@ import errno
import fnmatch import fnmatch
import os import os
import re import re
import sys from io import StringIO
if sys.version_info.major == 2:
from StringIO import StringIO
else:
from io import StringIO
def _RaiseNotFound(path): def _RaiseNotFound(path):

View File

@@ -38,19 +38,11 @@ if args:
def do(string): def do(string):
if options.stdout: if options.stdout:
if sys.version_info.major == 2: sys.stdout.buffer.write(string.upper().encode('utf-8'))
sys.stdout.write(string.upper()) sys.stdout.buffer.write(options.eol.encode('utf-8'))
sys.stdout.write(options.eol)
else:
sys.stdout.buffer.write(string.upper().encode('utf-8'))
sys.stdout.buffer.write(options.eol.encode('utf-8'))
if options.stderr: if options.stderr:
if sys.version_info.major == 2: sys.stderr.buffer.write(string.lower().encode('utf-8'))
sys.stderr.write(string.lower()) sys.stderr.buffer.write(options.eol.encode('utf-8'))
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.flush() sys.stderr.flush()

View File

@@ -11,13 +11,7 @@ import json
import os import os
import unittest import unittest
import sys import sys
from unittest import mock
if sys.version_info.major == 2:
import mock
BUILTIN_OPEN = '__builtin__.open'
else:
from unittest import mock
BUILTIN_OPEN = 'builtins.open'
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 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): class HasLuciContextLocalAuthTest(unittest.TestCase):
def setUp(self): def setUp(self):
mock.patch('os.environ').start() 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) self.addCleanup(mock.patch.stopall)
def testNoLuciContextEnvVar(self): def testNoLuciContextEnvVar(self):

View File

@@ -7,11 +7,7 @@ import os
import platform import platform
import sys import sys
import unittest import unittest
from unittest import mock
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

View File

@@ -13,15 +13,8 @@ import subprocess
import sys import sys
import tempfile import tempfile
import unittest import unittest
from io import StringIO
import distutils from unittest import mock
if sys.version_info.major == 2:
from StringIO import StringIO
import mock
else:
from io import StringIO
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

View File

@@ -39,14 +39,10 @@ class CheckCallAndFilterTestCase(unittest.TestCase):
self.printfn = io.StringIO() self.printfn = io.StringIO()
self.stdout = io.BytesIO() self.stdout = io.BytesIO()
self.kids = [] self.kids = []
if sys.version_info.major == 2: mock.patch('sys.stdout', mock.Mock()).start()
mock.patch('sys.stdout', self.stdout).start() mock.patch('sys.stdout.buffer', self.stdout).start()
mock.patch('__builtin__.print', self.printfn.write).start() mock.patch('sys.stdout.isatty', return_value=False).start()
else: 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() mock.patch('sys.stdout.flush', lambda: None).start()
self.addCleanup(mock.patch.stopall) self.addCleanup(mock.patch.stopall)

View File

@@ -11,13 +11,8 @@ import sys
import tempfile import tempfile
import threading import threading
import unittest import unittest
from unittest import mock
if sys.version_info.major == 2: import queue
import mock
import Queue
else:
from unittest import mock
import queue as Queue
DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, DEPOT_TOOLS_ROOT) sys.path.insert(0, DEPOT_TOOLS_ROOT)
@@ -48,9 +43,9 @@ class LockTest(unittest.TestCase):
to acquire the same file lock with timeout.''' to acquire the same file lock with timeout.'''
# Queues q_f1 and q_sleep are used to controll execution of individual # Queues q_f1 and q_sleep are used to controll execution of individual
# threads. # threads.
q_f1 = Queue.Queue() q_f1 = queue.Queue()
q_sleep = Queue.Queue() q_sleep = queue.Queue()
results = Queue.Queue() results = queue.Queue()
def side_effect(arg): def side_effect(arg):
'''side_effect is called when with l.lock is blocked. In this unit test '''side_effect is called when with l.lock is blocked. In this unit test

View File

@@ -11,16 +11,10 @@ import contextlib
import json import json
import logging import logging
import os import os
import requests
import sys import sys
import tempfile import tempfile
import time
import unittest import unittest
from unittest import mock
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

View File

@@ -9,11 +9,7 @@ import logging
import os import os
import sys import sys
import unittest import unittest
from unittest import mock
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

View File

@@ -6,11 +6,7 @@
import os import os
import sys import sys
import unittest import unittest
from unittest import mock
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) _ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, _ROOT) sys.path.insert(0, _ROOT)

View File

@@ -8,11 +8,7 @@
import os import os
import sys import sys
import unittest import unittest
from unittest import mock
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
DEPOT_TOOLS = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DEPOT_TOOLS = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, DEPOT_TOOLS) sys.path.insert(0, DEPOT_TOOLS)
@@ -84,11 +80,8 @@ class DefaultsTest(unittest.TestCase):
@mock.patch('subprocess.Popen.__init__') @mock.patch('subprocess.Popen.__init__')
def test_env_type(self, mockPopen): def test_env_type(self, mockPopen):
if sys.version_info.major != 2: subprocess2.Popen(['foo'], env={b'key': b'value'})
subprocess2.Popen(['foo'], env={b'key': b'value'}) mockPopen.assert_called_with(['foo'], env={'key': 'value'}, shell=mock.ANY)
mockPopen.assert_called_with(['foo'],
env={'key': 'value'},
shell=mock.ANY)
def _run_test(with_subprocess=True): def _run_test(with_subprocess=True):
@@ -130,13 +123,8 @@ class SmokeTests(unittest.TestCase):
def _check_exception(self, subp, e, stdout, stderr, returncode): def _check_exception(self, subp, e, stdout, stderr, returncode):
"""On exception, look if the exception members are set correctly.""" """On exception, look if the exception members are set correctly."""
self.assertEqual(returncode, e.returncode) self.assertEqual(returncode, e.returncode)
if subp is subprocess2 or sys.version_info.major == 3: self.assertEqual(stdout, e.stdout)
self.assertEqual(stdout, e.stdout) self.assertEqual(stderr, e.stderr)
self.assertEqual(stderr, e.stderr)
else:
# subprocess never save the output.
self.assertFalse(hasattr(e, 'stdout'))
self.assertFalse(hasattr(e, 'stderr'))
def test_check_output_no_stdout(self): def test_check_output_no_stdout(self):
for subp in (subprocess, subprocess2): for subp in (subprocess, subprocess2):

View File

@@ -7,11 +7,7 @@ import logging
import os import os
import sys import sys
import unittest import unittest
from unittest import mock
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DEPOT_TOOLS_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, DEPOT_TOOLS_ROOT) sys.path.insert(0, DEPOT_TOOLS_ROOT)

View File

@@ -10,11 +10,7 @@
import os import os
import sys import sys
import unittest import unittest
from unittest import mock
if sys.version_info.major == 2:
import mock
else:
from unittest import mock
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

View File

@@ -31,13 +31,9 @@ import subprocess
import sys import sys
import tempfile import tempfile
import time import time
if sys.version_info[0] < 3: from urllib.request import urlopen
from urllib2 import urlopen, URLError from urllib.parse import urljoin
from urlparse import urljoin from urllib.error import URLError
else:
from urllib.request import urlopen
from urllib.parse import urljoin
from urllib.error import URLError
import zipfile import zipfile
# Environment variable that, if set, specifies the default Visual Studio # Environment variable that, if set, specifies the default Visual Studio
@@ -185,10 +181,7 @@ def CalculateHash(root, expected_hash):
if expected_hash: if expected_hash:
path_without_hash = path_without_hash.replace( path_without_hash = path_without_hash.replace(
os.path.join(root, expected_hash).replace('/', '\\'), root) os.path.join(root, expected_hash).replace('/', '\\'), root)
if sys.version_info[0] < 3: digest.update(bytes(path_without_hash.lower(), 'utf-8'))
digest.update(path_without_hash.lower())
else:
digest.update(bytes(path_without_hash.lower(), 'utf-8'))
with open(path, 'rb') as f: with open(path, 'rb') as f:
digest.update(f.read()) digest.update(f.read())