mirror of
https://github.com/google/styleguide.git
synced 2024-03-22 13:11:43 +08:00
Add python 3 support to cpplint and cpplint_unittest
This commit is contained in:
parent
056962e73a
commit
9670c3d0b5
17
cpplint/cpplint.py
vendored
17
cpplint/cpplint.py
vendored
|
@ -53,6 +53,8 @@ import sys
|
|||
import unicodedata
|
||||
import sysconfig
|
||||
|
||||
import six
|
||||
|
||||
try:
|
||||
xrange # Python 2
|
||||
except NameError:
|
||||
|
@ -952,7 +954,7 @@ class _CppLintState(object):
|
|||
|
||||
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)
|
||||
|
@ -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'):
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
# 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')
|
||||
'replace'
|
||||
)
|
||||
|
||||
_cpplint_state.ResetErrorCounts()
|
||||
for filename in filenames:
|
||||
|
|
|
@ -3071,7 +3071,7 @@ class CpplintTest(CpplintTestBase):
|
|||
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(
|
||||
|
@ -3081,12 +3081,12 @@ class CpplintTest(CpplintTestBase):
|
|||
' (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
|
||||
|
@ -3104,7 +3104,7 @@ class CpplintTest(CpplintTestBase):
|
|||
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(),
|
||||
|
@ -5723,8 +5723,9 @@ class QuietTest(unittest.TestCase):
|
|||
|
||||
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)
|
||||
|
@ -5734,7 +5735,8 @@ class QuietTest(unittest.TestCase):
|
|||
|
||||
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)
|
||||
|
@ -5744,9 +5746,10 @@ class QuietTest(unittest.TestCase):
|
|||
|
||||
def testNonQuietWithoutErrors(self):
|
||||
# This will succeed. We filtered out all the known errors for that file.
|
||||
(return_code, output) = self._runCppLint('--filter=' +
|
||||
(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)
|
||||
|
@ -5758,10 +5761,11 @@ class QuietTest(unittest.TestCase):
|
|||
|
||||
def testQuietWithoutErrors(self):
|
||||
# This will succeed. We filtered out all the known errors for that file.
|
||||
(return_code, output) = self._runCppLint('--quiet',
|
||||
(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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user