cpplint: Fix Python 3 errors.

Bug: 1027160
Change-Id: I398d988ee896226f7fe1347a529cf5a17c944181
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1947171
Reviewed-by: Anthony Polito <apolito@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
changes/71/1947171/2
Edward Lemur 6 years ago committed by Commit Bot
parent 02b5af3575
commit 6d31ed5d63

53
cpplint.py vendored

@ -55,7 +55,7 @@ import sys
import unicodedata import unicodedata
_USAGE = """ _USAGE = r"""
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...] Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
[--counting=total|toplevel|detailed] [--root=subdir] [--counting=total|toplevel|detailed] [--root=subdir]
[--linelength=digits] [--linelength=digits]
@ -1058,7 +1058,7 @@ class FileInfo(object):
return os.path.abspath(self._filename).replace('\\', '/') return os.path.abspath(self._filename).replace('\\', '/')
def RepositoryName(self): def RepositoryName(self):
"""FullName after removing the local path to the repository. r"""FullName after removing the local path to the repository.
If we have a real absolute path name here we can try to do something smart: If we have a real absolute path name here we can try to do something smart:
detecting the root of the checkout and truncating /path/to/checkout from detecting the root of the checkout and truncating /path/to/checkout from
@ -1481,7 +1481,7 @@ def FindEndOfExpressionInLine(line, startpos, stack):
On finding an unclosed expression: (-1, None) On finding an unclosed expression: (-1, None)
Otherwise: (-1, new stack at end of this line) Otherwise: (-1, new stack at end of this line)
""" """
for i in xrange(startpos, len(line)): for i in range(startpos, len(line)):
char = line[i] char = line[i]
if char in '([{': if char in '([{':
# Found start of parenthesized expression, push to expression stack # Found start of parenthesized expression, push to expression stack
@ -1710,7 +1710,7 @@ def CheckForCopyright(filename, lines, error):
# We'll say it should occur by line 10. Don't forget there's a # We'll say it should occur by line 10. Don't forget there's a
# dummy line at the front. # dummy line at the front.
for line in xrange(1, min(len(lines), 11)): for line in range(1, min(len(lines), 11)):
if re.search(r'Copyright', lines[line], re.I): break if re.search(r'Copyright', lines[line], re.I): break
else: # means no copyright line was found else: # means no copyright line was found
error(filename, 0, 'legal/copyright', 5, error(filename, 0, 'legal/copyright', 5,
@ -1838,7 +1838,7 @@ def CheckForHeaderGuard(filename, clean_lines, error):
# contain any "//" comments at all, it could be that the compiler # contain any "//" comments at all, it could be that the compiler
# only wants "/**/" comments, look for those instead. # only wants "/**/" comments, look for those instead.
no_single_line_comments = True no_single_line_comments = True
for i in xrange(1, len(raw_lines) - 1): for i in range(1, len(raw_lines) - 1):
line = raw_lines[i] line = raw_lines[i]
if Match(r'^(?:(?:\'(?:\.|[^\'])*\')|(?:"(?:\.|[^"])*")|[^\'"])*//', line): if Match(r'^(?:(?:\'(?:\.|[^\'])*\')|(?:"(?:\.|[^"])*")|[^\'"])*//', line):
no_single_line_comments = False no_single_line_comments = False
@ -2179,7 +2179,7 @@ class _ClassInfo(_BlockInfo):
# If there is a DISALLOW macro, it should appear near the end of # If there is a DISALLOW macro, it should appear near the end of
# the class. # the class.
seen_last_thing_in_class = False seen_last_thing_in_class = False
for i in xrange(linenum - 1, self.starting_linenum, -1): for i in range(linenum - 1, self.starting_linenum, -1):
match = Search( match = Search(
r'\b(DISALLOW_COPY_AND_ASSIGN|DISALLOW_IMPLICIT_CONSTRUCTORS)\(' + r'\b(DISALLOW_COPY_AND_ASSIGN|DISALLOW_IMPLICIT_CONSTRUCTORS)\(' +
self.name + r'\)', self.name + r'\)',
@ -2961,7 +2961,7 @@ def CheckForFunctionLengths(filename, clean_lines, linenum,
if starting_func: if starting_func:
body_found = False body_found = False
for start_linenum in xrange(linenum, clean_lines.NumLines()): for start_linenum in range(linenum, clean_lines.NumLines()):
start_line = lines[start_linenum] start_line = lines[start_linenum]
joined_line += ' ' + start_line.lstrip() joined_line += ' ' + start_line.lstrip()
if Search(r'(;|})', start_line): # Declarations and trivial functions if Search(r'(;|})', start_line): # Declarations and trivial functions
@ -3418,7 +3418,7 @@ def _IsType(clean_lines, nesting_state, expr):
continue continue
# Look for typename in the specified range # Look for typename in the specified range
for i in xrange(first_line, last_line + 1, 1): for i in range(first_line, last_line + 1, 1):
if Search(typename_pattern, clean_lines.elided[i]): if Search(typename_pattern, clean_lines.elided[i]):
return True return True
block_index -= 1 block_index -= 1
@ -3482,7 +3482,7 @@ def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error):
trailing_text = '' trailing_text = ''
if endpos > -1: if endpos > -1:
trailing_text = endline[endpos:] trailing_text = endline[endpos:]
for offset in xrange(endlinenum + 1, for offset in range(endlinenum + 1,
min(endlinenum + 3, clean_lines.NumLines() - 1)): min(endlinenum + 3, clean_lines.NumLines() - 1)):
trailing_text += clean_lines.elided[offset] trailing_text += clean_lines.elided[offset]
# We also suppress warnings for `uint64_t{expression}` etc., as the style # We also suppress warnings for `uint64_t{expression}` etc., as the style
@ -4028,7 +4028,7 @@ def CheckCheck(filename, clean_lines, linenum, error):
expression = lines[linenum][start_pos + 1:end_pos - 1] expression = lines[linenum][start_pos + 1:end_pos - 1]
else: else:
expression = lines[linenum][start_pos + 1:] expression = lines[linenum][start_pos + 1:]
for i in xrange(linenum + 1, end_line): for i in range(linenum + 1, end_line):
expression += lines[i] expression += lines[i]
expression += last_line[0:end_pos - 1] expression += last_line[0:end_pos - 1]
@ -4156,7 +4156,7 @@ def GetLineWidth(line):
The width of the line in column positions, accounting for Unicode The width of the line in column positions, accounting for Unicode
combining characters and wide characters. combining characters and wide characters.
""" """
if isinstance(line, unicode): if sys.version_info == 2 and isinstance(line, unicode):
width = 0 width = 0
for uc in unicodedata.normalize('NFC', line): for uc in unicodedata.normalize('NFC', line):
if unicodedata.east_asian_width(uc) in ('W', 'F'): if unicodedata.east_asian_width(uc) in ('W', 'F'):
@ -4804,7 +4804,7 @@ def IsDerivedFunction(clean_lines, linenum):
virt-specifier. virt-specifier.
""" """
# Scan back a few lines for start of current function # Scan back a few lines for start of current function
for i in xrange(linenum, max(-1, linenum - 10), -1): for i in range(linenum, max(-1, linenum - 10), -1):
match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i]) match = Match(r'^([^()]*\w+)\(', clean_lines.elided[i])
if match: if match:
# Look for "override" after the matching closing parenthesis # Look for "override" after the matching closing parenthesis
@ -4825,7 +4825,7 @@ def IsOutOfLineMethodDefinition(clean_lines, linenum):
True if current line contains an out-of-line method definition. True if current line contains an out-of-line method definition.
""" """
# Scan back a few lines for start of current function # Scan back a few lines for start of current function
for i in xrange(linenum, max(-1, linenum - 10), -1): for i in range(linenum, max(-1, linenum - 10), -1):
if Match(r'^([^()]*\w+)\(', clean_lines.elided[i]): if Match(r'^([^()]*\w+)\(', clean_lines.elided[i]):
return Match(r'^[^()]*\w+::\w+\(', clean_lines.elided[i]) is not None return Match(r'^[^()]*\w+::\w+\(', clean_lines.elided[i]) is not None
return False return False
@ -4841,7 +4841,7 @@ def IsInitializerList(clean_lines, linenum):
True if current line appears to be inside constructor initializer True if current line appears to be inside constructor initializer
list, False otherwise. list, False otherwise.
""" """
for i in xrange(linenum, 1, -1): for i in range(linenum, 1, -1):
line = clean_lines.elided[i] line = clean_lines.elided[i]
if i == linenum: if i == linenum:
remove_function_body = Match(r'^(.*)\{\s*$', line) remove_function_body = Match(r'^(.*)\{\s*$', line)
@ -4942,7 +4942,7 @@ def CheckForNonConstReference(filename, clean_lines, linenum,
# Found the matching < on an earlier line, collect all # Found the matching < on an earlier line, collect all
# pieces up to current line. # pieces up to current line.
line = '' line = ''
for i in xrange(startline, linenum + 1): for i in range(startline, linenum + 1):
line += clean_lines.elided[i].strip() line += clean_lines.elided[i].strip()
# Check for non-const references in function parameters. A single '&' may # Check for non-const references in function parameters. A single '&' may
@ -4966,7 +4966,7 @@ def CheckForNonConstReference(filename, clean_lines, linenum,
# appear inside the second set of parentheses on the current line as # appear inside the second set of parentheses on the current line as
# opposed to the first set. # opposed to the first set.
if linenum > 0: if linenum > 0:
for i in xrange(linenum - 1, max(0, linenum - 10), -1): for i in range(linenum - 1, max(0, linenum - 10), -1):
previous_line = clean_lines.elided[i] previous_line = clean_lines.elided[i]
if not Search(r'[),]\s*$', previous_line): if not Search(r'[),]\s*$', previous_line):
break break
@ -4997,7 +4997,7 @@ def CheckForNonConstReference(filename, clean_lines, linenum,
# Don't see a whitelisted function on this line. Actually we # Don't see a whitelisted function on this line. Actually we
# didn't see any function name on this line, so this is likely a # didn't see any function name on this line, so this is likely a
# multi-line parameter list. Try a bit harder to catch this case. # multi-line parameter list. Try a bit harder to catch this case.
for i in xrange(2): for i in range(2):
if (linenum > i and if (linenum > i and
Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])): Search(whitelisted_functions, clean_lines.elided[linenum - i - 1])):
return return
@ -5160,7 +5160,7 @@ def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
# Try expanding current context to see if we one level of # Try expanding current context to see if we one level of
# parentheses inside a macro. # parentheses inside a macro.
if linenum > 0: if linenum > 0:
for i in xrange(linenum - 1, max(0, linenum - 5), -1): for i in range(linenum - 1, max(0, linenum - 5), -1):
context = clean_lines.elided[i] + context context = clean_lines.elided[i] + context
if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context): if Match(r'.*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$', context):
return False return False
@ -5379,7 +5379,7 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
required = {} # A map of header name to linenumber and the template entity. required = {} # A map of header name to linenumber and the template entity.
# Example of required: { '<functional>': (1219, 'less<>') } # Example of required: { '<functional>': (1219, 'less<>') }
for linenum in xrange(clean_lines.NumLines()): for linenum in range(clean_lines.NumLines()):
line = clean_lines.elided[linenum] line = clean_lines.elided[linenum]
if not line or line[0] == '#': if not line or line[0] == '#':
continue continue
@ -5515,7 +5515,7 @@ def CheckRedundantVirtual(filename, clean_lines, linenum, error):
end_col = -1 end_col = -1
end_line = -1 end_line = -1
start_col = len(virtual.group(2)) start_col = len(virtual.group(2))
for start_line in xrange(linenum, min(linenum + 3, clean_lines.NumLines())): for start_line in range(linenum, min(linenum + 3, clean_lines.NumLines())):
line = clean_lines.elided[start_line][start_col:] line = clean_lines.elided[start_line][start_col:]
parameter_list = Match(r'^([^(]*)\(', line) parameter_list = Match(r'^([^(]*)\(', line)
if parameter_list: if parameter_list:
@ -5530,7 +5530,7 @@ def CheckRedundantVirtual(filename, clean_lines, linenum, error):
# Look for "override" or "final" after the parameter list # Look for "override" or "final" after the parameter list
# (possibly on the next few lines). # (possibly on the next few lines).
for i in xrange(end_line, min(end_line + 3, clean_lines.NumLines())): for i in range(end_line, min(end_line + 3, clean_lines.NumLines())):
line = clean_lines.elided[i][end_col:] line = clean_lines.elided[i][end_col:]
match = Search(r'\b(override|final)\b', line) match = Search(r'\b(override|final)\b', line)
if match: if match:
@ -5787,7 +5787,7 @@ def ProcessFileData(filename, file_extension, lines, error,
if file_extension == 'h': if file_extension == 'h':
CheckForHeaderGuard(filename, clean_lines, error) CheckForHeaderGuard(filename, clean_lines, error)
for line in xrange(clean_lines.NumLines()): for line in range(clean_lines.NumLines()):
ProcessLine(filename, file_extension, clean_lines, line, ProcessLine(filename, file_extension, clean_lines, line,
include_state, function_state, nesting_state, error, include_state, function_state, nesting_state, error,
extra_check_functions) extra_check_functions)
@ -6075,10 +6075,11 @@ def main():
# Change stderr to write with replacement characters so we don't die # Change stderr to write with replacement characters so we don't die
# if we try to print something containing non-ASCII characters. # if we try to print something containing non-ASCII characters.
sys.stderr = codecs.StreamReaderWriter(sys.stderr, # We use sys.stderr.buffer in Python 3, since StreamReaderWriter writes bytes
codecs.getreader('utf8'), # to the specified stream.
codecs.getwriter('utf8'), sys.stderr = codecs.StreamReaderWriter(
'replace') getattr(sys.stderr, 'buffer', sys.stderr),
codecs.getreader('utf8'), codecs.getwriter('utf8'), 'replace')
_cpplint_state.ResetErrorCounts() _cpplint_state.ResetErrorCounts()
for filename in filenames: for filename in filenames:

Loading…
Cancel
Save