Skip to content

Commit

Permalink
Added python 3 compatibility to cpp_lint.py
Browse files Browse the repository at this point in the history
  • Loading branch information
willyd committed Mar 17, 2017
1 parent 317d162 commit 802d90f
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions scripts/cpp_lint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python2
#!/usr/bin/env python
#
# Copyright (c) 2009 Google Inc. All rights reserved.
#
Expand Down Expand Up @@ -52,6 +52,10 @@
import sys
import unicodedata

import six

from six import iteritems, itervalues
from six.moves import xrange

_USAGE = """
Syntax: cpp_lint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
Expand Down Expand Up @@ -756,7 +760,7 @@ def IncrementErrorCount(self, category):

def PrintErrorCounts(self):
"""Print a summary of errors by category, and the total."""
for category, count in self.errors_by_category.iteritems():
for category, count in iteritems(self.errors_by_category):
sys.stderr.write('Category \'%s\' errors found: %d\n' %
(category, count))
sys.stderr.write('Total errors found: %d\n' % self.error_count)
Expand Down Expand Up @@ -3444,16 +3448,16 @@ def GetLineWidth(line):
The width of the line in column positions, accounting for Unicode
combining characters and wide characters.
"""
if 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)
if six.PY2:
if 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
return len(line)


def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
Expand Down Expand Up @@ -3774,7 +3778,7 @@ def _GetTextInside(text, start_pattern):

# Give opening punctuations to get the matching close-punctuations.
matching_punctuation = {'(': ')', '{': '}', '[': ']'}
closing_punctuation = set(matching_punctuation.itervalues())
closing_punctuation = set(itervalues(matching_punctuation))

# Find the position to start extracting text.
match = re.search(start_pattern, text, re.M)
Expand Down Expand Up @@ -4851,10 +4855,11 @@ def main():

# Change stderr to write with replacement characters so we don't die
# if we try to print something containing non-ASCII characters.
sys.stderr = codecs.StreamReaderWriter(sys.stderr,
codecs.getreader('utf8'),
codecs.getwriter('utf8'),
'replace')
if six.PY2:
sys.stderr = codecs.StreamReaderWriter(sys.stderr,
codecs.getreader('utf8'),
codecs.getwriter('utf8'),
'replace')

_cpplint_state.ResetErrorCounts()
for filename in filenames:
Expand Down

0 comments on commit 802d90f

Please sign in to comment.