diff --git a/scripts/code_style.py b/scripts/code_style.py index d9c61a5af..d3ae624ec 100755 --- a/scripts/code_style.py +++ b/scripts/code_style.py @@ -18,7 +18,6 @@ This script must be run from the root of a Git work tree containing Mbed TLS. # See the License for the specific language governing permissions and # limitations under the License. import argparse -import io import os import re import subprocess @@ -29,12 +28,10 @@ UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1" CONFIG_FILE = ".uncrustify.cfg" UNCRUSTIFY_EXE = "uncrustify" UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE] -STDOUT_UTF8 = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') -STDERR_UTF8 = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh" def print_err(*args): - print("Error: ", *args, file=STDERR_UTF8) + print("Error: ", *args, file=sys.stderr) # Match FILENAME(s) in "check SCRIPT (FILENAME...)" CHECK_CALL_RE = re.compile(r"\n\s*check\s+[^\s#$&*?;|]+([^\n#$&*?;|]+)", @@ -67,8 +64,8 @@ def get_src_files() -> List[str]: "tests/suites/*.function", "scripts/data_files/*.fmt"] - result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE, \ - stderr=STDERR_UTF8, check=False) + result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE, + check=False) if result.returncode != 0: print_err("git ls-files returned: " + str(result.returncode)) @@ -118,7 +115,7 @@ def check_style_is_correct(src_file_list: List[str]) -> bool: cp = subprocess.run(diff_cmd, check=False) if cp.returncode == 1: - print(src_file + " changed - code style is incorrect.", file=STDOUT_UTF8) + print(src_file + " changed - code style is incorrect.") style_correct = False elif cp.returncode != 0: raise subprocess.CalledProcessError(cp.returncode, cp.args, @@ -136,8 +133,7 @@ def fix_style_single_pass(src_file_list: List[str]) -> bool: code_change_args = UNCRUSTIFY_ARGS + ["--no-backup"] for src_file in src_file_list: uncrustify_cmd = [UNCRUSTIFY_EXE] + code_change_args + [src_file] - result = subprocess.run(uncrustify_cmd, check=False, \ - stdout=STDOUT_UTF8, stderr=STDERR_UTF8) + result = subprocess.run(uncrustify_cmd, check=False) if result.returncode != 0: print_err("Uncrustify with file returned: " + \ str(result.returncode) + " correcting file " + \ @@ -169,9 +165,9 @@ def main() -> int: uncrustify_version = get_uncrustify_version().strip() if UNCRUSTIFY_SUPPORTED_VERSION not in uncrustify_version: print("Warning: Using unsupported Uncrustify version '" + - uncrustify_version + "'", file=STDOUT_UTF8) + uncrustify_version + "'") print("Note: The only supported version is " + - UNCRUSTIFY_SUPPORTED_VERSION, file=STDOUT_UTF8) + UNCRUSTIFY_SUPPORTED_VERSION) parser = argparse.ArgumentParser() parser.add_argument('-f', '--fix', action='store_true', @@ -200,8 +196,7 @@ def main() -> int: else: # Check mode if check_style_is_correct(src_files): - print("Checked {} files, style ok.".format(len(src_files)), - file=STDOUT_UTF8) + print("Checked {} files, style ok.".format(len(src_files))) return 0 else: return 1