In lint-format-strings, open files sequentially

In lint-format-strings, we use python argparse to read our file arguments. In
this mode, argparse opens all the files simultaneously. On OS X, where the
default filehandle limit is 128, this causes the lint to fail. Instead, ask
argparse for our filename arguments as strings, and open them one at a time
using 'with open'.
This commit is contained in:
Glenn Willen 2019-03-04 17:10:11 -08:00
parent 14023c966c
commit 21be609b49

View file

@ -262,27 +262,27 @@ def main():
parser.add_argument("--skip-arguments", type=int, help="number of arguments before the format string " parser.add_argument("--skip-arguments", type=int, help="number of arguments before the format string "
"argument (e.g. 1 in the case of fprintf)", default=0) "argument (e.g. 1 in the case of fprintf)", default=0)
parser.add_argument("function_name", help="function name (e.g. fprintf)", default=None) parser.add_argument("function_name", help="function name (e.g. fprintf)", default=None)
parser.add_argument("file", type=argparse.FileType("r", encoding="utf-8"), nargs="*", help="C++ source code file (e.g. foo.cpp)") parser.add_argument("file", nargs="*", help="C++ source code file (e.g. foo.cpp)")
args = parser.parse_args() args = parser.parse_args()
exit_code = 0 exit_code = 0
for f in args.file: for filename in args.file:
for function_call_str in parse_function_calls(args.function_name, f.read()): with open(filename, "r", encoding="utf-8") as f:
parts = parse_function_call_and_arguments(args.function_name, function_call_str) for function_call_str in parse_function_calls(args.function_name, f.read()):
relevant_function_call_str = unescape("".join(parts))[:512] parts = parse_function_call_and_arguments(args.function_name, function_call_str)
if (f.name, relevant_function_call_str) in FALSE_POSITIVES: relevant_function_call_str = unescape("".join(parts))[:512]
continue if (f.name, relevant_function_call_str) in FALSE_POSITIVES:
if len(parts) < 3 + args.skip_arguments: continue
exit_code = 1 if len(parts) < 3 + args.skip_arguments:
print("{}: Could not parse function call string \"{}(...)\": {}".format(f.name, args.function_name, relevant_function_call_str)) exit_code = 1
continue print("{}: Could not parse function call string \"{}(...)\": {}".format(f.name, args.function_name, relevant_function_call_str))
argument_count = len(parts) - 3 - args.skip_arguments continue
format_str = parse_string_content(parts[1 + args.skip_arguments]) argument_count = len(parts) - 3 - args.skip_arguments
format_specifier_count = count_format_specifiers(format_str) format_str = parse_string_content(parts[1 + args.skip_arguments])
if format_specifier_count != argument_count: format_specifier_count = count_format_specifiers(format_str)
exit_code = 1 if format_specifier_count != argument_count:
print("{}: Expected {} argument(s) after format string but found {} argument(s): {}".format(f.name, format_specifier_count, argument_count, relevant_function_call_str)) exit_code = 1
continue print("{}: Expected {} argument(s) after format string but found {} argument(s): {}".format(f.name, format_specifier_count, argument_count, relevant_function_call_str))
continue
sys.exit(exit_code) sys.exit(exit_code)