From 962d5fa3ec14d0a9cd58adae90feee67172284b5 Mon Sep 17 00:00:00 2001 From: Eric Cousineau Date: Sun, 7 Oct 2018 22:38:59 -0400 Subject: [PATCH] Make things consistent --- common/test_utilities/drake_py_unittest_main.py | 10 ++++------ tools/install/cpsutils.py | 3 +-- tools/lint/drakelint.py | 12 +++++------- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/common/test_utilities/drake_py_unittest_main.py b/common/test_utilities/drake_py_unittest_main.py index e81e948dc95c..03d5f21e1903 100644 --- a/common/test_utilities/drake_py_unittest_main.py +++ b/common/test_utilities/drake_py_unittest_main.py @@ -13,12 +13,10 @@ import six +if six.PY3: + _open = open -def _open(filename, mode): - if six.PY2: - return open(filename, mode) - else: - return open(filename, mode, encoding="utf-8") + def open(filename, mode="r"): return _open(filename, mode, encoding="utf8") if __name__ == '__main__': @@ -43,7 +41,7 @@ def _open(filename, mode): if not found_filename: raise RuntimeError("No such file found {}!".format( test_filename)) - with _open(found_filename, "r") as infile: + with open(found_filename, "r") as infile: for line in infile.readlines(): if any([line.startswith("if __name__ =="), line.strip().startswith("unittest.main")]): diff --git a/tools/install/cpsutils.py b/tools/install/cpsutils.py index 962bc06792d3..55f6c2694738 100644 --- a/tools/install/cpsutils.py +++ b/tools/install/cpsutils.py @@ -9,8 +9,7 @@ if sys.version_info.major >= 3: _open = open - def open(file, mode='r'): - return _open(file, mode, encoding='utf8') + def open(filename, mode="r"): return _open(filename, mode, encoding="utf8") def read_defs(pattern, groups=(1, 2)): diff --git a/tools/lint/drakelint.py b/tools/lint/drakelint.py index 4b018d6e0d13..d34e975b8494 100644 --- a/tools/lint/drakelint.py +++ b/tools/lint/drakelint.py @@ -5,12 +5,10 @@ from drake.tools.lint.formatter import IncludeFormatter +if sys.version_info.major >= 3: + _open = open -def _open(filename, mode): - if six.PY2: - return open(filename, mode) - else: - return open(filename, mode, encoding="utf-8") + def open(filename, mode="r"): return _open(filename, mode, encoding="utf8") def _check_invalid_line_endings(filename): @@ -18,7 +16,7 @@ def _check_invalid_line_endings(filename): otherwise. """ # Ask Python to read the file and determine the newlines convention. - with _open(filename, 'rU') as file: + with open(filename, 'rU') as file: if not file: print("ERROR: unable to open " + filename) return 1 @@ -57,7 +55,7 @@ def _check_shebang(filename): acceptable shebang lines, and 1 otherwise. """ is_executable = os.access(filename, os.X_OK) - with _open(filename, 'r') as file: + with open(filename, 'r') as file: shebang = file.readline().rstrip("\n") has_shebang = shebang.startswith("#!") if is_executable and not has_shebang: