From fa7d72bd1be9a45e8c09525aee68caad1e57963e Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Fri, 24 Oct 2025 17:51:48 +0200 Subject: [PATCH] lint: Drop check to enforce encoding to be specified in Python scripts The check was incomplete and brittle. A better check would be to enable `PYTHONWARNDEFAULTENCODING=1` https://docs.python.org/3/whatsnew/3.10.html#optional-encodingwarning-and-encoding-locale-option However, it is unclear what the goal of adding explicit encodings everywhere is, given that: * Most modern systems already have UTF-8 enabled by default, except for Windows. * Python 3.15 will likely enable it globally by default, according to https://peps.python.org/pep-0686/#abstract * Adding the explicit encodings will bloat all code for no benefit. So remove the lint check and drop all redundant encoding= kwargs. All encoding= that are set for a reason, are kept. --- test/lint/lint-python-utf8-encoding.py | 73 -------------------------- 1 file changed, 73 deletions(-) delete mode 100755 test/lint/lint-python-utf8-encoding.py diff --git a/test/lint/lint-python-utf8-encoding.py b/test/lint/lint-python-utf8-encoding.py deleted file mode 100755 index 8c9266470f1..00000000000 --- a/test/lint/lint-python-utf8-encoding.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright (c) 2018-2022 The Bitcoin Core developers -# Distributed under the MIT software license, see the accompanying -# file COPYING or http://www.opensource.org/licenses/mit-license.php. -# -# Make sure we explicitly open all text files using UTF-8 (or ASCII) encoding to -# avoid potential issues on the BSDs where the locale is not always set. - -import sys -import re - -from subprocess import check_output, CalledProcessError - -EXCLUDED_DIRS = ["src/crc32c/", "src/secp256k1/"] - - -def get_exclude_args(): - return [":(exclude)" + dir for dir in EXCLUDED_DIRS] - - -def check_fileopens(): - fileopens = list() - - try: - fileopens = check_output(["git", "grep", r" open(", "--", "*.py"] + get_exclude_args(), text=True, encoding="utf8").splitlines() - except CalledProcessError as e: - if e.returncode > 1: - raise e - - filtered_fileopens = [fileopen for fileopen in fileopens if not re.search(r"encoding=.(ascii|utf8|utf-8).|open\([^,]*, (\*\*kwargs|['\"][^'\"]*b[^'\"]*['\"])", fileopen)] - - return filtered_fileopens - - -def check_checked_outputs(): - checked_outputs = list() - - try: - checked_outputs = check_output(["git", "grep", "check_output(", "--", "*.py"] + get_exclude_args(), text=True, encoding="utf8").splitlines() - except CalledProcessError as e: - if e.returncode > 1: - raise e - - filtered_checked_outputs = [checked_output for checked_output in checked_outputs if re.search(r"text=True", checked_output) and not re.search(r"encoding=.(ascii|utf8|utf-8).", checked_output)] - - return filtered_checked_outputs - - -def main(): - exit_code = 0 - - nonexplicit_utf8_fileopens = check_fileopens() - if nonexplicit_utf8_fileopens: - print("Python's open(...) seems to be used to open text files without explicitly specifying encoding='utf8':\n") - for fileopen in nonexplicit_utf8_fileopens: - print(fileopen) - exit_code = 1 - - nonexplicit_utf8_checked_outputs = check_checked_outputs() - if nonexplicit_utf8_checked_outputs: - if nonexplicit_utf8_fileopens: - print("\n") - print("Python's check_output(...) seems to be used to get program outputs without explicitly specifying encoding='utf8':\n") - for checked_output in nonexplicit_utf8_checked_outputs: - print(checked_output) - exit_code = 1 - - sys.exit(exit_code) - - -if __name__ == "__main__": - main()