From 92a810c6d624f66abf2826bd2d93218dd6a98946 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 7 Oct 2025 22:19:49 +0200 Subject: [PATCH] Address unspecified-encoding. --- .pylintrc | 1 - plugins/module_utils/_api/auth.py | 4 ++-- plugins/module_utils/_api/context/api.py | 2 +- plugins/module_utils/_api/context/config.py | 6 +++--- plugins/module_utils/_api/context/context.py | 4 ++-- .../module_utils/_api/transport/sshconn.py | 2 +- plugins/module_utils/_api/utils/build.py | 2 +- plugins/module_utils/_api/utils/config.py | 2 +- plugins/module_utils/_api/utils/utils.py | 2 +- plugins/module_utils/_util.py | 2 +- plugins/modules/docker_image.py | 2 +- plugins/modules/docker_login.py | 2 +- .../plugins/module_utils/_api/test_auth.py | 20 +++++++++---------- .../module_utils/_api/utils/test_build.py | 8 ++++---- .../module_utils/_api/utils/test_config.py | 6 +++--- 15 files changed, 32 insertions(+), 33 deletions(-) diff --git a/.pylintrc b/.pylintrc index 4349367f..aa3b6352 100644 --- a/.pylintrc +++ b/.pylintrc @@ -410,7 +410,6 @@ disable=raw-checker-failed, unexpected-keyword-arg, unnecessary-dunder-call, unnecessary-pass, - unspecified-encoding, unsupported-assignment-operation, # TODO: needs better typing info unused-argument, unused-variable, diff --git a/plugins/module_utils/_api/auth.py b/plugins/module_utils/_api/auth.py index 8977ec93..0c63d48b 100644 --- a/plugins/module_utils/_api/auth.py +++ b/plugins/module_utils/_api/auth.py @@ -166,7 +166,7 @@ class AuthConfig(dict): if not config_file: return cls({}, credstore_env) try: - with open(config_file) as f: + with open(config_file, "rt", encoding="utf-8") as f: config_dict = json.load(f) except (IOError, KeyError, ValueError) as e: # Likely missing new Docker config file or it is in an @@ -351,7 +351,7 @@ def _load_legacy_config(config_file): log.debug("Attempting to parse legacy auth file format") try: data = [] - with open(config_file) as f: + with open(config_file, "rt", encoding="utf-8") as f: for line in f.readlines(): data.append(line.strip().split(" = ")[1]) if len(data) < 2: diff --git a/plugins/module_utils/_api/context/api.py b/plugins/module_utils/_api/context/api.py index 360392f8..fc1e1da1 100644 --- a/plugins/module_utils/_api/context/api.py +++ b/plugins/module_utils/_api/context/api.py @@ -151,7 +151,7 @@ class ContextAPI(object): if filename == METAFILE: filepath = os.path.join(dirname, filename) try: - with open(filepath, "r") as f: + with open(filepath, "rt", encoding="utf-8") as f: data = json.load(f) name = data["Name"] if name == "default": diff --git a/plugins/module_utils/_api/context/config.py b/plugins/module_utils/_api/context/config.py index 117d49bf..4b0357e9 100644 --- a/plugins/module_utils/_api/context/config.py +++ b/plugins/module_utils/_api/context/config.py @@ -32,7 +32,7 @@ def get_current_context_name_with_source(): docker_cfg_path = find_config_file() if docker_cfg_path: try: - with open(docker_cfg_path) as f: + with open(docker_cfg_path, "rt", encoding="utf-8") as f: return ( json.load(f).get("currentContext", "default"), f"configuration file {docker_cfg_path}", @@ -53,7 +53,7 @@ def write_context_name_to_docker_config(name=None): config = {} if docker_cfg_path: try: - with open(docker_cfg_path) as f: + with open(docker_cfg_path, "rt", encoding="utf-8") as f: config = json.load(f) except Exception as e: return e @@ -67,7 +67,7 @@ def write_context_name_to_docker_config(name=None): if not docker_cfg_path: docker_cfg_path = get_default_config_file() try: - with open(docker_cfg_path, "w") as f: + with open(docker_cfg_path, "wt", encoding="utf-8") as f: json.dump(config, f, indent=4) except Exception as e: return e diff --git a/plugins/module_utils/_api/context/context.py b/plugins/module_utils/_api/context/context.py index b1314052..f0d4c7d8 100644 --- a/plugins/module_utils/_api/context/context.py +++ b/plugins/module_utils/_api/context/context.py @@ -133,7 +133,7 @@ class Context(object): metadata = {} try: - with open(meta_file) as f: + with open(meta_file, "rt", encoding="utf-8") as f: metadata = json.load(f) except (OSError, KeyError, ValueError) as e: # unknown format @@ -189,7 +189,7 @@ class Context(object): meta_dir = get_meta_dir(self.name) if not os.path.isdir(meta_dir): os.makedirs(meta_dir) - with open(get_meta_file(self.name), "w") as f: + with open(get_meta_file(self.name), "wt", encoding="utf-8") as f: f.write(json.dumps(self.Metadata)) tls_dir = get_tls_dir(self.name) diff --git a/plugins/module_utils/_api/transport/sshconn.py b/plugins/module_utils/_api/transport/sshconn.py index 57ed17d4..0b30c12b 100644 --- a/plugins/module_utils/_api/transport/sshconn.py +++ b/plugins/module_utils/_api/transport/sshconn.py @@ -222,7 +222,7 @@ class SSHHTTPAdapter(BaseHTTPAdapter): ssh_config_file = os.path.expanduser("~/.ssh/config") if os.path.exists(ssh_config_file): conf = paramiko.SSHConfig() - with open(ssh_config_file) as f: + with open(ssh_config_file, "rt", encoding="utf-8") as f: conf.parse(f) host_config = conf.lookup(base_url.hostname) if "proxycommand" in host_config: diff --git a/plugins/module_utils/_api/utils/build.py b/plugins/module_utils/_api/utils/build.py index 9cb6948f..fc5f45a0 100644 --- a/plugins/module_utils/_api/utils/build.py +++ b/plugins/module_utils/_api/utils/build.py @@ -271,7 +271,7 @@ def process_dockerfile(dockerfile, path): 0 ] or os.path.relpath(abs_dockerfile, path).startswith(".."): # Dockerfile not in context - read data to insert into tar later - with open(abs_dockerfile) as df: + with open(abs_dockerfile, "rt", encoding="utf-8") as df: return (f".dockerfile.{random.getrandbits(160):x}", df.read()) # Dockerfile is inside the context - return path relative to context root diff --git a/plugins/module_utils/_api/utils/config.py b/plugins/module_utils/_api/utils/config.py index 0fce7087..6c2fa487 100644 --- a/plugins/module_utils/_api/utils/config.py +++ b/plugins/module_utils/_api/utils/config.py @@ -80,7 +80,7 @@ def load_general_config(config_path=None): return {} try: - with open(config_file) as f: + with open(config_file, "rt", encoding="utf-8") as f: return json.load(f) except (IOError, ValueError) as e: # In the case of a legacy `.dockercfg` file, we will not diff --git a/plugins/module_utils/_api/utils/utils.py b/plugins/module_utils/_api/utils/utils.py index 625143c8..7a455ea9 100644 --- a/plugins/module_utils/_api/utils/utils.py +++ b/plugins/module_utils/_api/utils/utils.py @@ -452,7 +452,7 @@ def parse_env_file(env_file): """ environment = {} - with open(env_file, "r") as f: + with open(env_file, "rt", encoding="utf-8") as f: for line in f: if line[0] == "#": diff --git a/plugins/module_utils/_util.py b/plugins/module_utils/_util.py index 4139a245..9de7dbfc 100644 --- a/plugins/module_utils/_util.py +++ b/plugins/module_utils/_util.py @@ -111,7 +111,7 @@ def log_debug(msg, pretty_print=False): If ``pretty_print=True``, the message will be pretty-printed as JSON. """ - with open("docker.log", "a") as log_file: + with open("docker.log", "at", encoding="utf-8") as log_file: if pretty_print: log_file.write( json.dumps(msg, sort_keys=True, indent=4, separators=(",", ": ")) diff --git a/plugins/modules/docker_image.py b/plugins/modules/docker_image.py index a1e7d5de..5cd052f4 100644 --- a/plugins/modules/docker_image.py +++ b/plugins/modules/docker_image.py @@ -891,7 +891,7 @@ class ImageManager(DockerBaseClass): dockerignore = os.path.join(self.build_path, ".dockerignore") exclude = None if os.path.exists(dockerignore): - with open(dockerignore) as f: + with open(dockerignore, "rt", encoding="utf-8") as f: exclude = list( filter( lambda x: x != "" and x[0] != "#", diff --git a/plugins/modules/docker_login.py b/plugins/modules/docker_login.py index 95609d37..88803916 100644 --- a/plugins/modules/docker_login.py +++ b/plugins/modules/docker_login.py @@ -161,7 +161,7 @@ class DockerFileStore(object): try: # Attempt to read the existing config. - with open(self._config_path, "r") as f: + with open(self._config_path, "rt", encoding="utf-8") as f: config = json.load(f) except (ValueError, IOError): # No config found or an invalid config found so we'll ignore it. diff --git a/tests/unit/plugins/module_utils/_api/test_auth.py b/tests/unit/plugins/module_utils/_api/test_auth.py index 4ea13bb0..b7be91ad 100644 --- a/tests/unit/plugins/module_utils/_api/test_auth.py +++ b/tests/unit/plugins/module_utils/_api/test_auth.py @@ -300,7 +300,7 @@ class LoadConfigTest(unittest.TestCase): self.addCleanup(shutil.rmtree, folder) cfg_path = os.path.join(folder, ".dockercfg") auth_ = base64.b64encode(b"sakuya:izayoi").decode("ascii") - with open(cfg_path, "w") as f: + with open(cfg_path, "wt", encoding="utf-8") as f: f.write(f"auth = {auth_}\n") f.write("email = sakuya@scarlet.net") @@ -319,7 +319,7 @@ class LoadConfigTest(unittest.TestCase): cfg_path = os.path.join(folder, ".dockercfg") auth_ = base64.b64encode(b"sakuya:izayoi").decode("ascii") email = "sakuya@scarlet.net" - with open(cfg_path, "w") as f: + with open(cfg_path, "wt", encoding="utf-8") as f: json.dump({auth.INDEX_URL: {"auth": auth_, "email": email}}, f) cfg = auth.load_config(cfg_path) assert auth.resolve_authconfig(cfg) is not None @@ -336,7 +336,7 @@ class LoadConfigTest(unittest.TestCase): cfg_path = os.path.join(folder, "config.json") auth_ = base64.b64encode(b"sakuya:izayoi").decode("ascii") email = "sakuya@scarlet.net" - with open(cfg_path, "w") as f: + with open(cfg_path, "wt", encoding="utf-8") as f: json.dump({"auths": {auth.INDEX_URL: {"auth": auth_, "email": email}}}, f) cfg = auth.load_config(cfg_path) assert auth.resolve_authconfig(cfg) is not None @@ -355,7 +355,7 @@ class LoadConfigTest(unittest.TestCase): auth_ = base64.b64encode(b"sakuya:izayoi").decode("ascii") config = {registry: {"auth": f"{auth_}", "email": "sakuya@scarlet.net"}} - with open(dockercfg_path, "w") as f: + with open(dockercfg_path, "wt", encoding="utf-8") as f: json.dump(config, f) cfg = auth.load_config(dockercfg_path).auths @@ -376,7 +376,7 @@ class LoadConfigTest(unittest.TestCase): auth_ = base64.b64encode(b"sakuya:izayoi").decode("ascii") config = {registry: {"auth": f"{auth_}", "email": "sakuya@scarlet.net"}} - with open(dockercfg_path, "w") as f: + with open(dockercfg_path, "wt", encoding="utf-8") as f: json.dump(config, f) with mock.patch.dict(os.environ, {"DOCKER_CONFIG": folder}): @@ -400,7 +400,7 @@ class LoadConfigTest(unittest.TestCase): "auths": {registry: {"auth": f"{auth_}", "email": "sakuya@scarlet.net"}} } - with open(dockercfg_path, "w") as f: + with open(dockercfg_path, "wt", encoding="utf-8") as f: json.dump(config, f) with mock.patch.dict(os.environ, {"DOCKER_CONFIG": folder}): @@ -423,7 +423,7 @@ class LoadConfigTest(unittest.TestCase): "auths": {registry: {"auth": f"{auth_}", "email": "sakuya@scarlet.net"}} } - with open(dockercfg_path, "w") as f: + with open(dockercfg_path, "wt", encoding="utf-8") as f: json.dump(config, f) with mock.patch.dict(os.environ, {"DOCKER_CONFIG": folder}): @@ -440,7 +440,7 @@ class LoadConfigTest(unittest.TestCase): self.addCleanup(shutil.rmtree, folder) dockercfg_path = os.path.join(folder, "config.json") config = {"detachKeys": "ctrl-q, ctrl-u, ctrl-i"} - with open(dockercfg_path, "w") as f: + with open(dockercfg_path, "wt", encoding="utf-8") as f: json.dump(config, f) cfg = auth.load_config(dockercfg_path) @@ -451,7 +451,7 @@ class LoadConfigTest(unittest.TestCase): self.addCleanup(shutil.rmtree, folder) dockercfg_path = os.path.join(folder, "config.json") config = {"auths": {"scarlet.net": {"sakuya": "izayoi"}}} - with open(dockercfg_path, "w") as f: + with open(dockercfg_path, "wt", encoding="utf-8") as f: json.dump(config, f) cfg = auth.load_config(dockercfg_path) @@ -465,7 +465,7 @@ class LoadConfigTest(unittest.TestCase): dockercfg_path = os.path.join(folder, "config.json") auth_entry = encode_auth({"username": "sakuya"}).decode("ascii") config = {"auths": {registry: {"auth": auth_entry, "identitytoken": token}}} - with open(dockercfg_path, "w") as f: + with open(dockercfg_path, "wt", encoding="utf-8") as f: json.dump(config, f) cfg = auth.load_config(dockercfg_path) diff --git a/tests/unit/plugins/module_utils/_api/utils/test_build.py b/tests/unit/plugins/module_utils/_api/utils/test_build.py index fb5e8878..ab35042d 100644 --- a/tests/unit/plugins/module_utils/_api/utils/test_build.py +++ b/tests/unit/plugins/module_utils/_api/utils/test_build.py @@ -37,7 +37,7 @@ def make_tree(dirs, files): os.makedirs(os.path.join(base, path)) for path in files: - with open(os.path.join(base, path), "w") as f: + with open(os.path.join(base, path), "wt", encoding="utf-8") as f: f.write("content") return base @@ -440,7 +440,7 @@ class TarTest(unittest.TestCase): base = tempfile.mkdtemp() full_path = os.path.join(base, "foo") self.addCleanup(shutil.rmtree, base) - with open(full_path, "w") as f: + with open(full_path, "wt", encoding="utf-8") as f: f.write("content") os.chmod(full_path, 0o222) with pytest.raises(IOError) as ei: @@ -452,7 +452,7 @@ class TarTest(unittest.TestCase): def test_tar_with_file_symlinks(self): base = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, base) - with open(os.path.join(base, "foo"), "w") as f: + with open(os.path.join(base, "foo"), "wt", encoding="utf-8") as f: f.write("content") os.makedirs(os.path.join(base, "bar")) os.symlink("../foo", os.path.join(base, "bar/foo")) @@ -500,7 +500,7 @@ class TarTest(unittest.TestCase): base = tempfile.mkdtemp() filename = os.path.join(base, "th.txt") self.addCleanup(shutil.rmtree, base) - with open(filename, "w") as f: + with open(filename, "wt", encoding="utf-8") as f: f.write("Invisible Full Moon") os.utime(filename, (12345, -3600.0)) with tar(base) as archive: diff --git a/tests/unit/plugins/module_utils/_api/utils/test_config.py b/tests/unit/plugins/module_utils/_api/utils/test_config.py index 250bb478..d0f414fa 100644 --- a/tests/unit/plugins/module_utils/_api/utils/test_config.py +++ b/tests/unit/plugins/module_utils/_api/utils/test_config.py @@ -96,7 +96,7 @@ class LoadConfigTest(unittest.TestCase): "HttpHeaders": {"Name": "Spike", "Surname": "Spiegel"}, } - with open(dockercfg_path, "w") as f: + with open(dockercfg_path, "wt", encoding="utf-8") as f: json.dump(config_data, f) cfg = config.load_general_config(dockercfg_path) @@ -108,7 +108,7 @@ class LoadConfigTest(unittest.TestCase): self.addCleanup(shutil.rmtree, folder) dockercfg_path = os.path.join(folder, "config.json") config_data = {"detachKeys": "ctrl-q, ctrl-u, ctrl-i"} - with open(dockercfg_path, "w") as f: + with open(dockercfg_path, "wt", encoding="utf-8") as f: json.dump(config_data, f) cfg = config.load_general_config(dockercfg_path) @@ -119,7 +119,7 @@ class LoadConfigTest(unittest.TestCase): self.addCleanup(shutil.rmtree, folder) dockercfg_path = os.path.join(folder, "config.json") config_data = {"detachKeys": "ctrl-q, ctrl-u, ctrl-i"} - with open(dockercfg_path, "w") as f: + with open(dockercfg_path, "wt", encoding="utf-8") as f: json.dump(config_data, f) with mock.patch.dict(os.environ, {"DOCKER_CONFIG": folder}):