From 2351b931dd1b3ca0c83b1d684d5a62ba90f9f280 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 5 Oct 2018 16:14:51 +0200 Subject: [PATCH 1/2] clint: use fileinput for stdin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes handing of "-" on Python 3: Traceback (most recent call last): File "…/Vcs/neovim/src/clint.py", line 3625, in main() File "…/Vcs/neovim/src/clint.py", line 3618, in main ProcessFile(filename, _cpplint_state.verbose_level) File "…/Vcs/neovim/src/clint.py", line 3464, in ProcessFile 'replace').read().split('\n') File "/usr/lib/python3.7/codecs.py", line 701, in read return self.reader.read(size) File "/usr/lib/python3.7/codecs.py", line 500, in read data = self.bytebuffer + newdata TypeError: can't concat str to bytes --- src/clint.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/clint.py b/src/clint.py index 9fd93ce143..cb08b52dc6 100755 --- a/src/clint.py +++ b/src/clint.py @@ -49,6 +49,7 @@ from __future__ import unicode_literals import codecs import copy +import fileinput import getopt import math # for log import os @@ -3456,10 +3457,10 @@ def ProcessFile(filename, vlevel, extra_check_functions=[]): # is processed. if filename == '-': - lines = codecs.StreamReaderWriter(sys.stdin, - codecs.getreader('utf8'), - codecs.getwriter('utf8'), - 'replace').read().split('\n') + stdin = sys.stdin.read() + if sys.version_info < (3, 0): + stdin = stdin.decode('utf8') + lines = stdin.split('\n') else: lines = codecs.open( filename, 'r', 'utf8', 'replace').read().split('\n') From 1e7eb20c91b95e14bcd6fe92e9af70b2759aa5bd Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 5 Oct 2018 16:16:09 +0200 Subject: [PATCH 2/2] clint: add support for --stdin-filename --- src/clint.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/clint.py b/src/clint.py index cb08b52dc6..34af5d15fd 100755 --- a/src/clint.py +++ b/src/clint.py @@ -66,7 +66,7 @@ _USAGE = """ Syntax: clint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...] [--counting=total|toplevel|detailed] [--root=subdir] [--linelength=digits] [--record-errors=file] - [--suppress-errors=file] + [--suppress-errors=file] [--stdin-filename=filename] [file] ... The style guidelines this tries to follow are those in @@ -168,6 +168,9 @@ Syntax: clint.py [--verbose=#] [--output=vs7] [--filter=-x,+y,...] suppress-errors=file Errors listed in the given file will not be reported. + + stdin-filename=filename + Use specified filename when reading from stdin (file "-"). """ # We categorize each error message we print. Here are the categories. @@ -3461,6 +3464,8 @@ def ProcessFile(filename, vlevel, extra_check_functions=[]): if sys.version_info < (3, 0): stdin = stdin.decode('utf8') lines = stdin.split('\n') + if _cpplint_state.stdin_filename is not None: + filename = _cpplint_state.stdin_filename else: lines = codecs.open( filename, 'r', 'utf8', 'replace').read().split('\n') @@ -3541,7 +3546,9 @@ def ParseArguments(args): 'linelength=', 'extensions=', 'record-errors=', - 'suppress-errors=']) + 'suppress-errors=', + 'stdin-filename=', + ]) except getopt.GetoptError: PrintUsage('Invalid arguments.') @@ -3551,6 +3558,7 @@ def ParseArguments(args): counting_style = '' record_errors_file = None suppress_errors_file = None + stdin_filename = None for (opt, val) in opts: if opt == '--help': @@ -3587,6 +3595,8 @@ def ParseArguments(args): record_errors_file = val elif opt == '--suppress-errors': suppress_errors_file = val + elif opt == '--stdin-filename': + stdin_filename = val if not filenames: PrintUsage('No files were specified.') @@ -3597,6 +3607,7 @@ def ParseArguments(args): _SetCountingStyle(counting_style) _SuppressErrorsFrom(suppress_errors_file) _RecordErrorsTo(record_errors_file) + _cpplint_state.stdin_filename = stdin_filename return filenames