57
57
_valid_extensions = set (['c' , 'cc' , 'cpp' , 'cxx' , 'c++' , 'h' , 'hpp' , 'hxx' ,
58
58
'h++' ])
59
59
60
+ Py3k = (sys .version_info [0 ] == 3 )
61
+ """A boolean to check if we are running Python3000"""
62
+
60
63
_USAGE = """
61
64
Syntax: cpplint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...]
62
65
[--counting=total|toplevel|detailed] [--root=subdir]
502
505
# This is set by --linelength flag.
503
506
_line_length = 80
504
507
508
+ try :
509
+ xrange (0 ,1 )
510
+ except NameError :
511
+ xrange = range
512
+ try :
513
+ unicode
514
+ except NameError :
515
+ basestring = unicode = str
516
+ try :
517
+ long
518
+ except NameError :
519
+ long = int
520
+
505
521
def ParseNolintSuppressions (filename , raw_line , linenum , error ):
506
522
"""Updates the global list of error-suppressions.
507
523
@@ -841,7 +857,11 @@ def IncrementErrorCount(self, category):
841
857
842
858
def PrintErrorCounts (self ):
843
859
"""Print a summary of errors by category, and the total."""
844
- for category , count in self .errors_by_category .iteritems ():
860
+ try :
861
+ items = self .errors_by_category .iteritems ()
862
+ except AttributeError :
863
+ items = self .errors_by_category .items ()
864
+ for category , count in items :
845
865
sys .stderr .write ('Category \' %s\' errors found: %d\n ' %
846
866
(category , count ))
847
867
sys .stderr .write ('Total errors found: %d\n ' % self .error_count )
@@ -1816,7 +1836,7 @@ def CheckForBadCharacters(filename, lines, error):
1816
1836
error: The function to call with any errors found.
1817
1837
"""
1818
1838
for linenum , line in enumerate (lines ):
1819
- if u' \ufffd ' in line :
1839
+ if unicode ( b' \xef \xbf \xbd ' , 'utf-8' ) in line :
1820
1840
error (filename , linenum , 'readability/utf8' , 5 ,
1821
1841
'Line contains invalid UTF-8 (or Unicode replacement character).' )
1822
1842
if '\0 ' in line :
@@ -4702,7 +4722,10 @@ def _GetTextInside(text, start_pattern):
4702
4722
4703
4723
# Give opening punctuations to get the matching close-punctuations.
4704
4724
matching_punctuation = {'(' : ')' , '{' : '}' , '[' : ']' }
4705
- closing_punctuation = set (matching_punctuation .itervalues ())
4725
+ try :
4726
+ closing_punctuation = set (matching_punctuation .values ())
4727
+ except AttributeError :
4728
+ closing_punctuation = set (matching_punctuation .itervalues ())
4706
4729
4707
4730
# Find the position to start extracting text.
4708
4731
match = re .search (start_pattern , text , re .M )
@@ -5672,7 +5695,7 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error,
5672
5695
5673
5696
# include_dict is modified during iteration, so we iterate over a copy of
5674
5697
# the keys.
5675
- header_keys = include_dict .keys ()
5698
+ header_keys = list ( include_dict .keys () )
5676
5699
for header in header_keys :
5677
5700
(same_module , common_path ) = FilesBelongToSameModule (abs_filename , header )
5678
5701
fullpath = common_path + header
@@ -6307,10 +6330,11 @@ def main():
6307
6330
6308
6331
# Change stderr to write with replacement characters so we don't die
6309
6332
# if we try to print something containing non-ASCII characters.
6310
- sys .stderr = codecs .StreamReaderWriter (sys .stderr ,
6311
- codecs .getreader ('utf8' ),
6312
- codecs .getwriter ('utf8' ),
6313
- 'replace' )
6333
+ if not Py3k :
6334
+ sys .stderr = codecs .StreamReaderWriter (sys .stderr ,
6335
+ codecs .getreader ('utf8' ),
6336
+ codecs .getwriter ('utf8' ),
6337
+ 'replace' )
6314
6338
6315
6339
_cpplint_state .ResetErrorCounts ()
6316
6340
for filename in filenames :
@@ -6322,3 +6346,4 @@ def main():
6322
6346
6323
6347
if __name__ == '__main__' :
6324
6348
main ()
6349
+
0 commit comments