From c03476ac51b94cd136c39bb9c48fee4f1a815b42 Mon Sep 17 00:00:00 2001 From: Corey Oordt Date: Sat, 10 Feb 2024 08:24:36 -0600 Subject: [PATCH] Fix encoding when reading text. Fixes #68 --- bumpversion/config/create.py | 3 ++- bumpversion/config/files.py | 8 ++++++-- bumpversion/config/files_legacy.py | 3 ++- bumpversion/files.py | 3 ++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/bumpversion/config/create.py b/bumpversion/config/create.py index edf51024..8020d961 100644 --- a/bumpversion/config/create.py +++ b/bumpversion/config/create.py @@ -1,4 +1,5 @@ """Module for creating a new config file.""" + from pathlib import Path from typing import Tuple @@ -57,7 +58,7 @@ def get_defaults_from_dest(destination: str) -> Tuple[dict, TOMLDocument]: config = DEFAULTS.copy() if Path(destination).exists(): - destination_config = parse(Path(destination).read_text()) + destination_config = parse(Path(destination).read_text(encoding="utf-8")) else: destination_config = document() diff --git a/bumpversion/config/files.py b/bumpversion/config/files.py index 7aa68dce..2fcad842 100644 --- a/bumpversion/config/files.py +++ b/bumpversion/config/files.py @@ -39,7 +39,11 @@ def find_config_file(explicit_file: Union[str, Path, None] = None) -> Union[Path [Path(explicit_file)] if explicit_file else [Path.cwd().joinpath(path) for path in CONFIG_FILE_SEARCH_ORDER] ) return next( - (cfg_file for cfg_file in search_paths if cfg_file.exists() and "bumpversion]" in cfg_file.read_text()), + ( + cfg_file + for cfg_file in search_paths + if cfg_file.exists() and "bumpversion]" in cfg_file.read_text(encoding="utf-8") + ), None, ) @@ -91,7 +95,7 @@ def read_toml_file(file_path: Path) -> Dict[str, Any]: import tomlkit # Load the TOML file - toml_data = tomlkit.parse(file_path.read_text()).unwrap() + toml_data = tomlkit.parse(file_path.read_text(encoding="utf-8")).unwrap() return toml_data.get("tool", {}).get("bumpversion", {}) diff --git a/bumpversion/config/files_legacy.py b/bumpversion/config/files_legacy.py index 5a9ea4de..19e5a1a1 100644 --- a/bumpversion/config/files_legacy.py +++ b/bumpversion/config/files_legacy.py @@ -1,4 +1,5 @@ """This module handles the legacy config file format.""" + from __future__ import annotations import re @@ -102,7 +103,7 @@ def update_ini_config_file( ) config_path = Path(config_file) - existing_config = config_path.read_text() + existing_config = config_path.read_text(encoding="utf-8") if config_path.suffix == ".cfg" and cfg_current_version_regex.search(existing_config): sub_str = f"\\g{new_version}" new_config = cfg_current_version_regex.sub(sub_str, existing_config) diff --git a/bumpversion/files.py b/bumpversion/files.py index 6a5eb015..f3c3fcc0 100644 --- a/bumpversion/files.py +++ b/bumpversion/files.py @@ -1,4 +1,5 @@ """Methods for changing files.""" + import os.path import re from copy import deepcopy @@ -331,7 +332,7 @@ def _update_toml_file( """Update a TOML file.""" import tomlkit - toml_data = tomlkit.parse(self.path.read_text()) + toml_data = tomlkit.parse(self.path.read_text(encoding="utf-8")) value_before = get_nested_value(toml_data, self.file_change.key_path) if value_before is None: