From 1946224c650af29ff56abf5fb4130233655e16c1 Mon Sep 17 00:00:00 2001 From: Anghelo Carvajal Date: Sun, 29 Mar 2026 13:28:09 -0300 Subject: [PATCH 1/2] Explicit UTF-8 for everything --- CHANGELOG.md | 4 ++++ README.md | 2 +- pyproject.toml | 2 +- src/splat/__init__.py | 2 +- src/splat/scripts/create_config.py | 18 +++++++++++------- src/splat/scripts/split.py | 14 ++++++++------ src/splat/segtypes/common/bin.py | 7 +++---- src/splat/segtypes/common/c.py | 17 +++++++---------- src/splat/segtypes/common/codesubsegment.py | 4 ++-- src/splat/segtypes/common/databin.py | 2 +- src/splat/segtypes/common/header.py | 3 +-- src/splat/segtypes/common/rodatabin.py | 2 +- src/splat/segtypes/common/textbin.py | 2 +- src/splat/segtypes/linker_entry.py | 5 ++--- src/splat/segtypes/n64/decompressor.py | 15 +++++++-------- src/splat/segtypes/n64/gfx.py | 8 ++++---- src/splat/segtypes/n64/vtx.py | 8 ++++---- src/splat/util/conf.py | 4 ++-- src/splat/util/file_presets.py | 6 ++---- src/splat/util/n64/find_code_length.py | 4 ++-- src/splat/util/relocs.py | 2 +- src/splat/util/symbols.py | 2 +- test.py | 6 +++--- 23 files changed, 70 insertions(+), 69 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86491eaa..c0d6a3bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # splat Release Notes +### 0.39.1 + +* Make sure all plain text file read and writes have an explicit UTF-8 encoding. + ### 0.39.0 * New `o` segment allowing for a more granular way to include library objects. diff --git a/README.md b/README.md index 87974a9d..1b9fcc14 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The brackets corresponds to the optional dependencies to install while installin If you use a `requirements.txt` file in your repository, then you can add this library with the following line: ```txt -splat64[mips]>=0.39.0,<1.0.0 +splat64[mips]>=0.39.1,<1.0.0 ``` ### Optional dependencies diff --git a/pyproject.toml b/pyproject.toml index a135d7eb..0994b85b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "splat64" # Should be synced with src/splat/__init__.py -version = "0.39.0" +version = "0.39.1" description = "A binary splitting tool to assist with decompilation and modding projects" readme = "README.md" license = {file = "LICENSE"} diff --git a/src/splat/__init__.py b/src/splat/__init__.py index 8a5f6a51..f1d3e40f 100644 --- a/src/splat/__init__.py +++ b/src/splat/__init__.py @@ -1,7 +1,7 @@ __package_name__ = __name__ # Should be synced with pyproject.toml -__version__ = "0.39.0" +__version__ = "0.39.1" __author__ = "ethteck" from . import util as util diff --git a/src/splat/scripts/create_config.py b/src/splat/scripts/create_config.py index eed507cd..56ef8652 100644 --- a/src/splat/scripts/create_config.py +++ b/src/splat/scripts/create_config.py @@ -182,7 +182,7 @@ def create_n64_config(rom_path: Path): """ out_file = Path(f"{cleaned_basename}.yaml") - with out_file.open("w", newline="\n") as f: + with out_file.open("w", encoding="utf-8", newline="\n") as f: print(f"Writing config to {out_file}") f.write(header) f.write(segments) @@ -247,7 +247,7 @@ def create_n64_config(rom_path: Path): ) reloc_addrs.append("") if reloc_addrs: - with Path("reloc_addrs.txt").open("w", newline="\n") as f: + with Path("reloc_addrs.txt").open("w", encoding="utf-8", newline="\n") as f: print("Writing reloc_addrs.txt") f.write( "// Visit https://github.com/ethteck/splat/wiki/Advanced-Reloc for documentation about this file\n" @@ -265,7 +265,7 @@ def create_n64_config(rom_path: Path): ) if symbol_addrs: symbol_addrs.append("") - with Path("symbol_addrs.txt").open("w", newline="\n") as f: + with Path("symbol_addrs.txt").open("w", encoding="utf-8", newline="\n") as f: print("Writing symbol_addrs.txt") f.write( "// Visit https://github.com/ethteck/splat/wiki/Adding-Symbols for documentation about this file\n" @@ -363,7 +363,7 @@ def create_psx_config(exe_path: Path, exe_bytes: bytes): """ out_file = Path(f"{cleaned_basename}.yaml") - with out_file.open("w", newline="\n") as f: + with out_file.open("w", encoding="utf-8", newline="\n") as f: print(f"Writing config to {out_file}") f.write(header) f.write(segments) @@ -478,7 +478,7 @@ def do_elf(elf_path: Path, elf_bytes: bytes, objcopy: Optional[str]): """ out_file = Path(f"{cleaned_basename}.yaml") - with out_file.open("w", newline="\n") as f: + with out_file.open("w", encoding="utf-8", newline="\n") as f: print(f"Writing config to {out_file}") f.write(header) f.write(segments) @@ -493,7 +493,7 @@ def do_elf(elf_path: Path, elf_bytes: bytes, objcopy: Optional[str]): symbol_addrs.append(f"_start = 0x{elf.entrypoint:08X}; // type:func") if symbol_addrs: symbol_addrs.append("") - with Path("symbol_addrs.txt").open("w", newline="\n") as f: + with Path("symbol_addrs.txt").open("w", encoding="utf-8", newline="\n") as f: print("Writing symbol_addrs.txt") f.write( "// Visit https://github.com/ethteck/splat/wiki/Adding-Symbols for documentation about this file\n" @@ -506,7 +506,11 @@ def do_elf(elf_path: Path, elf_bytes: bytes, objcopy: Optional[str]): linker_script.append("ENTRY(_start);") if linker_script: linker_script.append("") - with Path("linker_script_extra.ld").open("w", newline="\n") as f: + with Path("linker_script_extra.ld").open( + "w", + encoding="utf-8", + newline="\n", + ) as f: print("Writing linker_script_extra.ld") f.write( "/* Pass this file to the linker with the `-T linker_script_extra.ld` flag */\n" diff --git a/src/splat/scripts/split.py b/src/splat/scripts/split.py index a4fc643b..282e6cbb 100644 --- a/src/splat/scripts/split.py +++ b/src/splat/scripts/split.py @@ -354,8 +354,7 @@ def do_split( if segment.should_split(): segment_bytes = rom_bytes if segment.file_path: - with open(segment.file_path, "rb") as segment_input_file: - segment_bytes = segment_input_file.read() + segment_bytes = segment.file_path.read_bytes() segment.split(segment_bytes) @@ -461,13 +460,16 @@ def write_elf_sections_file(all_segments: List[Segment]): for segment in all_segments: section_list += "." + segment.get_cname() + "\n" options.opts.elf_section_list_path.parent.mkdir(parents=True, exist_ok=True) - with options.opts.elf_section_list_path.open("w", newline="\n") as f: - f.write(section_list) + options.opts.elf_section_list_path.write_text( + section_list, + encoding="utf-8", + newline="\n", + ) def write_undefined_auto(to_write: List[symbols.Symbol], file_path: Path): file_path.parent.mkdir(parents=True, exist_ok=True) - with file_path.open("w", newline="\n") as f: + with file_path.open("w", encoding="utf-8", newline="\n") as f: for symbol in to_write: f.write(f"{symbol.name} = 0x{symbol.vram_start:X};\n") @@ -518,7 +520,7 @@ def dump_symbols() -> None: splat_hidden_folder = options.opts.base_path / ".splat" splat_hidden_folder.mkdir(parents=True, exist_ok=True) - with open(splat_hidden_folder / "splat_symbols.csv", "w") as f: + with open(splat_hidden_folder / "splat_symbols.csv", "w", encoding="utf-8") as f: f.write( "vram_start,given_name,name,type,given_size,size,rom,defined,user_declared,referenced,extract\n" ) diff --git a/src/splat/segtypes/common/bin.py b/src/splat/segtypes/common/bin.py index 7c4b80d9..bf3699b8 100644 --- a/src/splat/segtypes/common/bin.py +++ b/src/splat/segtypes/common/bin.py @@ -28,11 +28,10 @@ def split(self, rom_bytes): if self.size is None or self.size <= 0: log.error(f"Segment {self.name} has zero size.") - with open(path, "wb") as f: - assert isinstance(self.rom_start, int) - assert isinstance(self.rom_end, int) + assert isinstance(self.rom_start, int) + assert isinstance(self.rom_end, int) + path.write_bytes(rom_bytes[self.rom_start : self.rom_end]) - f.write(rom_bytes[self.rom_start : self.rom_end]) self.log(f"Wrote {self.name} to {path}") @property diff --git a/src/splat/segtypes/common/c.py b/src/splat/segtypes/common/c.py index 77990eb4..a1ef18cd 100644 --- a/src/splat/segtypes/common/c.py +++ b/src/splat/segtypes/common/c.py @@ -51,8 +51,7 @@ def replacer(match): @staticmethod def get_funcs_defined_in_c(c_file: Path) -> Set[str]: - with open(c_file, "r", encoding="utf-8") as f: - text = CommonSegC.strip_c_comments(f.read()) + text = CommonSegC.strip_c_comments(c_file.read_text(encoding="utf-8")) return set(m.group(1) for m in C_FUNC_RE.finditer(text)) @@ -105,8 +104,7 @@ def find_include_rodata(text: str): @staticmethod def get_global_asm_funcs(c_file: Path) -> Set[str]: - with c_file.open(encoding="utf-8") as f: - text = CommonSegC.strip_c_comments(f.read()) + text = CommonSegC.strip_c_comments(c_file.read_text(encoding="utf-8")) if options.opts.compiler == IDO: return set(m.group(2) for m in C_GLOBAL_ASM_IDO_RE.finditer(text)) else: @@ -114,8 +112,7 @@ def get_global_asm_funcs(c_file: Path) -> Set[str]: @staticmethod def get_global_asm_rodata_syms(c_file: Path) -> Set[str]: - with c_file.open(encoding="utf-8") as f: - text = CommonSegC.strip_c_comments(f.read()) + text = CommonSegC.strip_c_comments(c_file.read_text(encoding="utf-8")) if options.opts.compiler == IDO: return set(m.group(2) for m in C_GLOBAL_ASM_IDO_RE.finditer(text)) else: @@ -351,7 +348,7 @@ def create_c_asm_file( outpath.parent.mkdir(parents=True, exist_ok=True) - with outpath.open("w", newline="\n") as f: + with outpath.open("w", encoding="utf-8", newline="\n") as f: if options.opts.asm_inc_header: f.write( options.opts.c_newline.join(options.opts.asm_inc_header.split("\n")) @@ -389,7 +386,7 @@ def create_unmigrated_rodata_file( outpath.parent.mkdir(parents=True, exist_ok=True) - with outpath.open("w", newline="\n") as f: + with outpath.open("w", encoding="utf-8", newline="\n") as f: preamble = options.opts.generated_s_preamble if preamble: f.write(preamble + "\n") @@ -480,8 +477,8 @@ def create_c_file( c_lines += self.get_c_lines_for_rodata_sym(rodata_sym, asm_out_dir) c_path.parent.mkdir(parents=True, exist_ok=True) - with c_path.open("w", newline=options.opts.c_newline) as f: - f.write("\n".join(c_lines)) + text = "\n".join(c_lines) + c_path.write_text(text, encoding="utf-8", newline=options.opts.c_newline) log.write(f"Wrote {self.name} to {c_path}") def create_asm_dependencies_file( diff --git a/src/splat/segtypes/common/codesubsegment.py b/src/splat/segtypes/common/codesubsegment.py index 18a5ea74..369a621e 100644 --- a/src/splat/segtypes/common/codesubsegment.py +++ b/src/splat/segtypes/common/codesubsegment.py @@ -245,7 +245,7 @@ def split_as_asm_file(self, out_path: Optional[Path]): self.print_file_boundaries() - with open(out_path, "w", newline="\n") as f: + with out_path.open("w", encoding="utf-8", newline="\n") as f: # Write `.text` contents for line in self.get_asm_file_header(): f.write(line + "\n") @@ -257,7 +257,7 @@ def split_as_asmtu_file(self, out_path: Path): self.print_file_boundaries() - with open(out_path, "w", newline="\n") as f: + with open(out_path, "w", encoding="utf-8", newline="\n") as f: # self.spim_section would be None if the current section was # declared `auto` in the yaml. # This can be useful when there are TUs with only data/rodata/bss/etc diff --git a/src/splat/segtypes/common/databin.py b/src/splat/segtypes/common/databin.py index 25ffd257..550c0cb7 100644 --- a/src/splat/segtypes/common/databin.py +++ b/src/splat/segtypes/common/databin.py @@ -35,7 +35,7 @@ def split(self, rom_bytes): assert s_path is not None s_path.parent.mkdir(parents=True, exist_ok=True) - with s_path.open("w") as f: + with s_path.open("w", encoding="utf-8") as f: f.write('.include "macro.inc"\n\n') preamble = options.opts.generated_s_preamble if preamble: diff --git a/src/splat/segtypes/common/header.py b/src/splat/segtypes/common/header.py index f8c41c66..0f358edd 100644 --- a/src/splat/segtypes/common/header.py +++ b/src/splat/segtypes/common/header.py @@ -37,8 +37,7 @@ def split(self, rom_bytes): src_path = self.out_path() src_path.parent.mkdir(parents=True, exist_ok=True) - with open(src_path, "w", newline="\n") as f: - f.write("\n".join(header_lines)) + src_path.write_text("\n".join(header_lines), encoding="utf-8", newline="\n") self.log(f"Wrote {self.name} to {src_path}") @staticmethod diff --git a/src/splat/segtypes/common/rodatabin.py b/src/splat/segtypes/common/rodatabin.py index f3e8575a..8219c5e3 100644 --- a/src/splat/segtypes/common/rodatabin.py +++ b/src/splat/segtypes/common/rodatabin.py @@ -35,7 +35,7 @@ def split(self, rom_bytes): assert s_path is not None s_path.parent.mkdir(parents=True, exist_ok=True) - with s_path.open("w") as f: + with s_path.open("w", encoding="utf-8") as f: f.write('.include "macro.inc"\n\n') preamble = options.opts.generated_s_preamble if preamble: diff --git a/src/splat/segtypes/common/textbin.py b/src/splat/segtypes/common/textbin.py index e24eb6f7..2b1db88e 100644 --- a/src/splat/segtypes/common/textbin.py +++ b/src/splat/segtypes/common/textbin.py @@ -153,7 +153,7 @@ def split(self, rom_bytes): s_path.parent.mkdir(parents=True, exist_ok=True) - with s_path.open("w") as f: + with s_path.open("w", encoding="utf-8") as f: f.write('.include "macro.inc"\n\n') preamble = options.opts.generated_s_preamble if preamble: diff --git a/src/splat/segtypes/linker_entry.py b/src/splat/segtypes/linker_entry.py index 192a203d..adc3329f 100644 --- a/src/splat/segtypes/linker_entry.py +++ b/src/splat/segtypes/linker_entry.py @@ -45,14 +45,13 @@ def path_to_object_path(path: Path) -> Path: def write_file_if_different(path: Path, new_content: str): if path.exists(): - old_content = path.read_text() + old_content = path.read_text(encoding="utf-8") else: old_content = "" if old_content != new_content: path.parent.mkdir(parents=True, exist_ok=True) - with path.open("w", newline=options.opts.c_newline) as f: - f.write(new_content) + path.write_text(new_content, encoding="utf-8", newline=options.opts.c_newline) def get_segment_rom_start(cname: str) -> str: diff --git a/src/splat/segtypes/n64/decompressor.py b/src/splat/segtypes/n64/decompressor.py index 1d992212..94369129 100644 --- a/src/splat/segtypes/n64/decompressor.py +++ b/src/splat/segtypes/n64/decompressor.py @@ -14,14 +14,13 @@ def split(self, rom_bytes): ) out_path = out_dir / f"{self.name}.bin" - with open(out_path, "wb") as f: - assert isinstance(self.rom_start, int) - assert isinstance(self.rom_end, int) - - self.log(f"Decompressing {self.name}") - compressed_bytes = rom_bytes[self.rom_start : self.rom_end] - decompressed_bytes = self.decompress(compressed_bytes) - f.write(decompressed_bytes) + assert isinstance(self.rom_start, int) + assert isinstance(self.rom_end, int) + + self.log(f"Decompressing {self.name}") + compressed_bytes = rom_bytes[self.rom_start : self.rom_end] + decompressed_bytes = self.decompress(compressed_bytes) + out_path.write_bytes(decompressed_bytes) self.log(f"Wrote {self.name} to {out_path}") def get_linker_entries(self): diff --git a/src/splat/segtypes/n64/gfx.py b/src/splat/segtypes/n64/gfx.py index dbf0afd3..68a73a57 100644 --- a/src/splat/segtypes/n64/gfx.py +++ b/src/splat/segtypes/n64/gfx.py @@ -259,11 +259,11 @@ def light_sub_func(match): return out_str def split(self, rom_bytes: bytes): - if self.file_text and self.out_path(): - self.out_path().parent.mkdir(parents=True, exist_ok=True) + out_path = self.out_path() + if self.file_text and out_path: + out_path.parent.mkdir(parents=True, exist_ok=True) - with open(self.out_path(), "w", newline="\n") as f: - f.write(self.file_text) + out_path.write_text(self.file_text, encoding="utf-8", newline="\n") def should_scan(self) -> bool: return ( diff --git a/src/splat/segtypes/n64/vtx.py b/src/splat/segtypes/n64/vtx.py index acf4bcbb..347ae2a1 100644 --- a/src/splat/segtypes/n64/vtx.py +++ b/src/splat/segtypes/n64/vtx.py @@ -89,11 +89,11 @@ def disassemble_data(self, rom_bytes) -> str: return "\n".join(lines) def split(self, rom_bytes: bytes): - if self.file_text and self.out_path(): - self.out_path().parent.mkdir(parents=True, exist_ok=True) + out_path = self.out_path() + if self.file_text and out_path: + out_path.parent.mkdir(parents=True, exist_ok=True) - with open(self.out_path(), "w", newline="\n") as f: - f.write(self.file_text) + out_path.write_text(self.file_text, encoding="utf-8", newline="\n") def should_scan(self) -> bool: return options.opts.is_mode_active("vtx") diff --git a/src/splat/util/conf.py b/src/splat/util/conf.py index ea45069b..2877c45a 100644 --- a/src/splat/util/conf.py +++ b/src/splat/util/conf.py @@ -78,8 +78,8 @@ def load( config: Dict[str, Any] = {} for entry in config_path: - with entry.open() as f: - additional_config = yaml.load(f.read(), Loader=yaml.SafeLoader) + entry_text = entry.read_text(encoding="utf-8") + additional_config = yaml.load(entry_text, Loader=yaml.SafeLoader) config = _merge_configs(config, additional_config, entry) vram_classes.initialize(config.get("vram_classes")) diff --git a/src/splat/util/file_presets.py b/src/splat/util/file_presets.py index 7965f68b..21b3ebcd 100644 --- a/src/splat/util/file_presets.py +++ b/src/splat/util/file_presets.py @@ -28,12 +28,10 @@ def _write(filepath: str, contents: str): p.parent.mkdir(parents=True, exist_ok=True) if p.exists(): - with p.open("r", encoding="UTF-8") as f: - existing_contents = f.read() + existing_contents = p.read_text(encoding="utf-8") if existing_contents == contents: return - with p.open("w", encoding="UTF-8", newline="\n") as f: - f.write(contents) + p.write_text(contents, encoding="utf-8", newline="\n") def write_include_asm_h(): diff --git a/src/splat/util/n64/find_code_length.py b/src/splat/util/n64/find_code_length.py index 1f9053ca..8400a776 100755 --- a/src/splat/util/n64/find_code_length.py +++ b/src/splat/util/n64/find_code_length.py @@ -1,6 +1,7 @@ #! /usr/bin/env python3 import argparse +from pathlib import Path import rabbitizer import spimdisasm @@ -49,8 +50,7 @@ def run(rom_bytes, start_offset, vram, end_offset=None): def main(): args = parser.parse_args() - with open(args.rom, "rb") as f: - rom_bytes = f.read() + rom_bytes = Path(args.rom).read_bytes() start = args.start end = args.end diff --git a/src/splat/util/relocs.py b/src/splat/util/relocs.py index 37a7a01a..10a7a7b3 100644 --- a/src/splat/util/relocs.py +++ b/src/splat/util/relocs.py @@ -31,7 +31,7 @@ def initialize(): if not path.exists(): continue - with path.open() as f: + with path.open(encoding="utf-8") as f: sym_addrs_lines = f.readlines() prog_bar = progress_bar.get_progress_bar(sym_addrs_lines) diff --git a/src/splat/util/symbols.py b/src/splat/util/symbols.py index cc38bb10..600856c4 100644 --- a/src/splat/util/symbols.py +++ b/src/splat/util/symbols.py @@ -344,7 +344,7 @@ def initialize(all_segments: "List[Segment]"): # Manual list of func name / addrs for path in options.opts.symbol_addrs_paths: if path.exists(): - with open(path) as f: + with open(path, encoding="utf-8") as f: sym_addrs_lines = f.readlines() handle_sym_addrs(path, sym_addrs_lines, all_segments) diff --git a/test.py b/test.py index d38eac4d..f1c69732 100755 --- a/test.py +++ b/test.py @@ -22,7 +22,7 @@ class Testing(unittest.TestCase): def compare_files(self, test_path, ref_path): - with io.open(test_path) as test_f, io.open(ref_path) as ref_f: + with io.open(test_path, encoding="utf-8") as test_f, io.open(ref_path, encoding="utf-8") as ref_f: self.assertListEqual(list(test_f), list(ref_f)) def get_same_files(self, dcmp: filecmp.dircmp, out: List[Tuple[str, str, str]]): @@ -103,9 +103,9 @@ def test_basic_app(self): remove_from_diff.add(file) continue - with open(f"{file[1]}/{file[0]}") as file1: + with open(f"{file[1]}/{file[0]}", encoding="utf-8") as file1: file1_lines = file1.readlines() - with open(f"{file[2]}/{file[0]}") as file2: + with open(f"{file[2]}/{file[0]}", encoding="utf-8") as file2: file2_lines = file2.readlines() for line in difflib.unified_diff( From 8ccccebb5f8529b1ffed8631140823d965d33ab1 Mon Sep 17 00:00:00 2001 From: Anghelo Carvajal Date: Sun, 29 Mar 2026 14:00:41 -0300 Subject: [PATCH 2/2] Avoid using write_text because python 3.9 does not support the newline parameter --- src/splat/scripts/split.py | 7 ++++--- src/splat/segtypes/common/c.py | 4 ++-- src/splat/segtypes/common/header.py | 3 ++- src/splat/segtypes/linker_entry.py | 3 ++- src/splat/segtypes/n64/gfx.py | 3 ++- src/splat/segtypes/n64/vtx.py | 3 ++- src/splat/util/file_presets.py | 3 ++- test.py | 5 ++++- 8 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/splat/scripts/split.py b/src/splat/scripts/split.py index 282e6cbb..98191acf 100644 --- a/src/splat/scripts/split.py +++ b/src/splat/scripts/split.py @@ -460,11 +460,12 @@ def write_elf_sections_file(all_segments: List[Segment]): for segment in all_segments: section_list += "." + segment.get_cname() + "\n" options.opts.elf_section_list_path.parent.mkdir(parents=True, exist_ok=True) - options.opts.elf_section_list_path.write_text( - section_list, + with options.opts.elf_section_list_path.open( + "w", encoding="utf-8", newline="\n", - ) + ) as f: + f.write(section_list) def write_undefined_auto(to_write: List[symbols.Symbol], file_path: Path): diff --git a/src/splat/segtypes/common/c.py b/src/splat/segtypes/common/c.py index a1ef18cd..7cbf0b23 100644 --- a/src/splat/segtypes/common/c.py +++ b/src/splat/segtypes/common/c.py @@ -477,8 +477,8 @@ def create_c_file( c_lines += self.get_c_lines_for_rodata_sym(rodata_sym, asm_out_dir) c_path.parent.mkdir(parents=True, exist_ok=True) - text = "\n".join(c_lines) - c_path.write_text(text, encoding="utf-8", newline=options.opts.c_newline) + with c_path.open("w", encoding="utf-8", newline=options.opts.c_newline) as f: + f.write("\n".join(c_lines)) log.write(f"Wrote {self.name} to {c_path}") def create_asm_dependencies_file( diff --git a/src/splat/segtypes/common/header.py b/src/splat/segtypes/common/header.py index 0f358edd..c0c2b1ad 100644 --- a/src/splat/segtypes/common/header.py +++ b/src/splat/segtypes/common/header.py @@ -37,7 +37,8 @@ def split(self, rom_bytes): src_path = self.out_path() src_path.parent.mkdir(parents=True, exist_ok=True) - src_path.write_text("\n".join(header_lines), encoding="utf-8", newline="\n") + with src_path.open("w", encoding="utf-8", newline="\n") as f: + f.write("\n".join(header_lines)) self.log(f"Wrote {self.name} to {src_path}") @staticmethod diff --git a/src/splat/segtypes/linker_entry.py b/src/splat/segtypes/linker_entry.py index adc3329f..be7c3851 100644 --- a/src/splat/segtypes/linker_entry.py +++ b/src/splat/segtypes/linker_entry.py @@ -51,7 +51,8 @@ def write_file_if_different(path: Path, new_content: str): if old_content != new_content: path.parent.mkdir(parents=True, exist_ok=True) - path.write_text(new_content, encoding="utf-8", newline=options.opts.c_newline) + with path.open("w", encoding="utf-8", newline=options.opts.c_newline) as f: + f.write(new_content) def get_segment_rom_start(cname: str) -> str: diff --git a/src/splat/segtypes/n64/gfx.py b/src/splat/segtypes/n64/gfx.py index 68a73a57..1ef52346 100644 --- a/src/splat/segtypes/n64/gfx.py +++ b/src/splat/segtypes/n64/gfx.py @@ -263,7 +263,8 @@ def split(self, rom_bytes: bytes): if self.file_text and out_path: out_path.parent.mkdir(parents=True, exist_ok=True) - out_path.write_text(self.file_text, encoding="utf-8", newline="\n") + with out_path.open("w", encoding="utf-8", newline="\n") as f: + f.write(self.file_text) def should_scan(self) -> bool: return ( diff --git a/src/splat/segtypes/n64/vtx.py b/src/splat/segtypes/n64/vtx.py index 347ae2a1..4f54726f 100644 --- a/src/splat/segtypes/n64/vtx.py +++ b/src/splat/segtypes/n64/vtx.py @@ -93,7 +93,8 @@ def split(self, rom_bytes: bytes): if self.file_text and out_path: out_path.parent.mkdir(parents=True, exist_ok=True) - out_path.write_text(self.file_text, encoding="utf-8", newline="\n") + with out_path.open("w", encoding="utf-8", newline="\n") as f: + f.write(self.file_text) def should_scan(self) -> bool: return options.opts.is_mode_active("vtx") diff --git a/src/splat/util/file_presets.py b/src/splat/util/file_presets.py index 21b3ebcd..ea940ea6 100644 --- a/src/splat/util/file_presets.py +++ b/src/splat/util/file_presets.py @@ -31,7 +31,8 @@ def _write(filepath: str, contents: str): existing_contents = p.read_text(encoding="utf-8") if existing_contents == contents: return - p.write_text(contents, encoding="utf-8", newline="\n") + with p.open("w", encoding="utf-8", newline="\n") as f: + f.write(contents) def write_include_asm_h(): diff --git a/test.py b/test.py index f1c69732..f1dfc0ba 100755 --- a/test.py +++ b/test.py @@ -22,7 +22,10 @@ class Testing(unittest.TestCase): def compare_files(self, test_path, ref_path): - with io.open(test_path, encoding="utf-8") as test_f, io.open(ref_path, encoding="utf-8") as ref_f: + with ( + io.open(test_path, encoding="utf-8") as test_f, + io.open(ref_path, encoding="utf-8") as ref_f, + ): self.assertListEqual(list(test_f), list(ref_f)) def get_same_files(self, dcmp: filecmp.dircmp, out: List[Tuple[str, str, str]]):