Skip to content

Commit 9670c3d

Browse files
committed
Add python 3 support to cpplint and cpplint_unittest
1 parent 056962e commit 9670c3d

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

cpplint/cpplint.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import unicodedata
5454
import sysconfig
5555

56+
import six
57+
5658
try:
5759
xrange # Python 2
5860
except NameError:
@@ -952,7 +954,7 @@ def IncrementErrorCount(self, category):
952954

953955
def PrintErrorCounts(self):
954956
"""Print a summary of errors by category, and the total."""
955-
for category, count in self.errors_by_category.iteritems():
957+
for category, count in six.iteritems(self.errors_by_category):
956958
sys.stderr.write('Category \'%s\' errors found: %d\n' %
957959
(category, count))
958960
sys.stdout.write('Total errors found: %d\n' % self.error_count)
@@ -4286,7 +4288,7 @@ def GetLineWidth(line):
42864288
The width of the line in column positions, accounting for Unicode
42874289
combining characters and wide characters.
42884290
"""
4289-
if isinstance(line, unicode):
4291+
if isinstance(line, six.text_type):
42904292
width = 0
42914293
for uc in unicodedata.normalize('NFC', line):
42924294
if unicodedata.east_asian_width(uc) in ('W', 'F'):
@@ -4622,7 +4624,7 @@ def _GetTextInside(text, start_pattern):
46224624

46234625
# Give opening punctuations to get the matching close-punctuations.
46244626
matching_punctuation = {'(': ')', '{': '}', '[': ']'}
4625-
closing_punctuation = set(matching_punctuation.itervalues())
4627+
closing_punctuation = set(six.itervalues(matching_punctuation))
46264628

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

55715573
# include_dict is modified during iteration, so we iterate over a copy of
55725574
# the keys.
5573-
header_keys = include_dict.keys()
5575+
header_keys = list(six.iterkeys(include_dict))
55745576
for header in header_keys:
55755577
(same_module, common_path) = FilesBelongToSameModule(abs_filename, header)
55765578
fullpath = common_path + header
@@ -6225,10 +6227,13 @@ def main():
62256227

62266228
# Change stderr to write with replacement characters so we don't die
62276229
# if we try to print something containing non-ASCII characters.
6228-
sys.stderr = codecs.StreamReaderWriter(sys.stderr,
6229-
codecs.getreader('utf8'),
6230-
codecs.getwriter('utf8'),
6231-
'replace')
6230+
# https://docs.python.org/3/library/sys.html#sys.stderr
6231+
sys.stderr = codecs.StreamReaderWriter(
6232+
sys.stderr if sys.version_info.major < 3 else sys.stderr.buffer,
6233+
codecs.getreader('utf8'),
6234+
codecs.getwriter('utf8'),
6235+
'replace'
6236+
)
62326237

62336238
_cpplint_state.ResetErrorCounts()
62346239
for filename in filenames:

cpplint/cpplint_unittest.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -3071,7 +3071,7 @@ def DoTest(self, raw_bytes, has_invalid_utf8):
30713071
error_collector = ErrorCollector(self.assert_)
30723072
cpplint.ProcessFileData(
30733073
'foo.cc', 'cc',
3074-
unicode(raw_bytes, 'utf8', 'replace').split('\n'),
3074+
raw_bytes.decode('utf-8', errors='replace').split('\n'),
30753075
error_collector)
30763076
# The warning appears only once.
30773077
self.assertEquals(
@@ -3081,12 +3081,12 @@ def DoTest(self, raw_bytes, has_invalid_utf8):
30813081
' (or Unicode replacement character).'
30823082
' [readability/utf8] [5]'))
30833083

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

30913091
def testBadCharacters(self):
30923092
# Test for NUL bytes only
@@ -3104,7 +3104,7 @@ def testBadCharacters(self):
31043104
cpplint.ProcessFileData(
31053105
'nul_utf8.cc', 'cc',
31063106
['// Copyright 2014 Your Company.',
3107-
unicode('\xe9x\0', 'utf8', 'replace'), ''],
3107+
b'\xe9x\0'.decode('utf-8', errors='replace'), ''],
31083108
error_collector)
31093109
self.assertEquals(
31103110
error_collector.Results(),
@@ -5723,8 +5723,9 @@ def _runCppLint(self, *args):
57235723

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

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

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

57595762
def testQuietWithoutErrors(self):
57605763
# This will succeed. We filtered out all the known errors for that file.
5761-
(return_code, output) = self._runCppLint('--quiet',
5762-
'--filter=' +
5763-
'-legal/copyright,' +
5764-
'-build/header_guard')
5764+
(return_code, output_bytes) = self._runCppLint('--quiet',
5765+
'--filter=' +
5766+
'-legal/copyright,' +
5767+
'-build/header_guard')
5768+
output = output_bytes.decode('utf-8')
57655769
self.assertEquals(0, return_code, output)
57665770
# No cpplint errors are printed since there were no errors.
57675771
self.assertNotIn("[legal/copyright]", output)

0 commit comments

Comments
 (0)