Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python 3 support to cpplint and cpplint_unittest #528

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions cpplint/cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import unicodedata
import sysconfig

import six

try:
xrange # Python 2
except NameError:
Expand Down Expand Up @@ -952,7 +954,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 six.iteritems(self.errors_by_category):
sys.stderr.write('Category \'%s\' errors found: %d\n' %
(category, count))
sys.stdout.write('Total errors found: %d\n' % self.error_count)
Expand Down Expand Up @@ -4286,7 +4288,7 @@ def GetLineWidth(line):
The width of the line in column positions, accounting for Unicode
combining characters and wide characters.
"""
if isinstance(line, unicode):
if isinstance(line, six.text_type):
width = 0
for uc in unicodedata.normalize('NFC', line):
if unicodedata.east_asian_width(uc) in ('W', 'F'):
Expand Down Expand Up @@ -4622,7 +4624,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(six.itervalues(matching_punctuation))

# Find the position to start extracting text.
match = re.search(start_pattern, text, re.M)
Expand Down Expand Up @@ -5570,7 +5572,7 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,

# include_dict is modified during iteration, so we iterate over a copy of
# the keys.
header_keys = include_dict.keys()
header_keys = list(six.iterkeys(include_dict))
for header in header_keys:
(same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
fullpath = common_path + header
Expand Down Expand Up @@ -6225,10 +6227,13 @@ 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')
# https://docs.python.org/3/library/sys.html#sys.stderr
sys.stderr = codecs.StreamReaderWriter(
sys.stderr if sys.version_info.major < 3 else sys.stderr.buffer,
codecs.getreader('utf8'),
codecs.getwriter('utf8'),
'replace'
)

_cpplint_state.ResetErrorCounts()
for filename in filenames:
Expand Down
34 changes: 19 additions & 15 deletions cpplint/cpplint_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3071,7 +3071,7 @@ def DoTest(self, raw_bytes, has_invalid_utf8):
error_collector = ErrorCollector(self.assert_)
cpplint.ProcessFileData(
'foo.cc', 'cc',
unicode(raw_bytes, 'utf8', 'replace').split('\n'),
raw_bytes.decode('utf-8', errors='replace').split('\n'),
error_collector)
# The warning appears only once.
self.assertEquals(
Expand All @@ -3081,12 +3081,12 @@ def DoTest(self, raw_bytes, has_invalid_utf8):
' (or Unicode replacement character).'
' [readability/utf8] [5]'))

DoTest(self, 'Hello world\n', False)
DoTest(self, '\xe9\x8e\xbd\n', False)
DoTest(self, '\xe9x\x8e\xbd\n', True)
DoTest(self, b'Hello world\n', False)
DoTest(self, b'\xe9\x8e\xbd\n', False)
DoTest(self, b'\xe9x\x8e\xbd\n', True)
# This is the encoding of the replacement character itself (which
# you can see by evaluating codecs.getencoder('utf8')(u'\ufffd')).
DoTest(self, '\xef\xbf\xbd\n', True)
DoTest(self, b'\xef\xbf\xbd\n', True)

def testBadCharacters(self):
# Test for NUL bytes only
Expand All @@ -3104,7 +3104,7 @@ def testBadCharacters(self):
cpplint.ProcessFileData(
'nul_utf8.cc', 'cc',
['// Copyright 2014 Your Company.',
unicode('\xe9x\0', 'utf8', 'replace'), ''],
b'\xe9x\0'.decode('utf-8', errors='replace'), ''],
error_collector)
self.assertEquals(
error_collector.Results(),
Expand Down Expand Up @@ -5723,8 +5723,9 @@ def _runCppLint(self, *args):

def testNonQuietWithErrors(self):
# This will fail: the test header is missing a copyright and header guard.
(return_code, output) = self._runCppLint()
(return_code, output_bytes) = self._runCppLint()
self.assertEquals(1, return_code)
output = output_bytes.decode('utf-8')
# Always-on behavior: Print error messages as they come up.
self.assertIn("[legal/copyright]", output)
self.assertIn("[build/header_guard]", output)
Expand All @@ -5734,7 +5735,8 @@ def testNonQuietWithErrors(self):

def testQuietWithErrors(self):
# When there are errors, behavior is identical to not passing --quiet.
(return_code, output) = self._runCppLint('--quiet')
(return_code, output_bytes) = self._runCppLint('--quiet')
output = output_bytes.decode('utf-8')
self.assertEquals(1, return_code)
self.assertIn("[legal/copyright]", output)
self.assertIn("[build/header_guard]", output)
Expand All @@ -5744,9 +5746,10 @@ def testQuietWithErrors(self):

def testNonQuietWithoutErrors(self):
# This will succeed. We filtered out all the known errors for that file.
(return_code, output) = self._runCppLint('--filter=' +
'-legal/copyright,' +
'-build/header_guard')
(return_code, output_bytes) = self._runCppLint('--filter=' +
'-legal/copyright,' +
'-build/header_guard')
output = output_bytes.decode('utf-8')
self.assertEquals(0, return_code, output)
# No cpplint errors are printed since there were no errors.
self.assertNotIn("[legal/copyright]", output)
Expand All @@ -5758,10 +5761,11 @@ def testNonQuietWithoutErrors(self):

def testQuietWithoutErrors(self):
# This will succeed. We filtered out all the known errors for that file.
(return_code, output) = self._runCppLint('--quiet',
'--filter=' +
'-legal/copyright,' +
'-build/header_guard')
(return_code, output_bytes) = self._runCppLint('--quiet',
'--filter=' +
'-legal/copyright,' +
'-build/header_guard')
output = output_bytes.decode('utf-8')
self.assertEquals(0, return_code, output)
# No cpplint errors are printed since there were no errors.
self.assertNotIn("[legal/copyright]", output)
Expand Down