From 61d220b4fa8f0b48699ab28c554c6f9d5e344c9b Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Sun, 21 Apr 2024 12:11:16 +0100 Subject: [PATCH] Simplify conditional encoding in install_scripts and easy_install --- setuptools/command/easy_install.py | 8 ++------ setuptools/command/install_scripts.py | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/setuptools/command/easy_install.py b/setuptools/command/easy_install.py index bfacf1e46f..41ff382fe4 100644 --- a/setuptools/command/easy_install.py +++ b/setuptools/command/easy_install.py @@ -874,12 +874,8 @@ def write_script(self, script_name, contents, mode="t", blockers=()): if os.path.exists(target): os.unlink(target) - if "b" not in mode and isinstance(contents, str): - kw = {"encoding": "utf-8"} - else: - kw = {} - - with open(target, "w" + mode, **kw) as f: + encoding = None if "b" in mode else "utf-8" + with open(target, "w" + mode, encoding=encoding) as f: f.write(contents) chmod(target, 0o777 - mask) diff --git a/setuptools/command/install_scripts.py b/setuptools/command/install_scripts.py index 758937b614..d79a4ab7b0 100644 --- a/setuptools/command/install_scripts.py +++ b/setuptools/command/install_scripts.py @@ -57,14 +57,10 @@ def write_script(self, script_name, contents, mode="t", *ignored): target = os.path.join(self.install_dir, script_name) self.outfiles.append(target) - if "b" not in mode and isinstance(contents, str): - kw = {"encoding": "utf-8"} - else: - kw = {} - + encoding = None if "b" in mode else "utf-8" mask = current_umask() if not self.dry_run: ensure_directory(target) - with open(target, "w" + mode, **kw) as f: + with open(target, "w" + mode, encoding=encoding) as f: f.write(contents) chmod(target, 0o777 - mask)