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>
changes/74/4824474/8
Gavin Mak 2 years ago committed by LUCI CQ
parent 7e25126240
commit 512f3cb37e

30
cpplint.py vendored

@ -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)

@ -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)

@ -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 <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():
"""Works around <http://bugs.python.org/issue6058>."""
# <http://msdn.microsoft.com/en-us/library/dd317756.aspx>
@ -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

@ -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()

@ -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."
"""

@ -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):

@ -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()

@ -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):

@ -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__))))

@ -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__))))

@ -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)

@ -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

@ -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__))))

@ -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__))))

@ -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)

@ -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):

@ -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)

@ -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__))))

@ -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())

Loading…
Cancel
Save