Skip to content

Commit

Permalink
Be explicit about encoding
Browse files Browse the repository at this point in the history
Default encoding is not always UTF-8, make it explicit. Also use
the surrogateescape error handler to preserve undecodable bytes.

Signed-off-by: Nikola Forró <nforro@redhat.com>
  • Loading branch information
nforro committed Jun 11, 2023
1 parent 3de61ff commit c5dce21
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions specfile/specfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(
"""
self.autosave = autosave
self._path = Path(path)
self._lines = self.path.read_text().splitlines()
self._lines = self._read_lines(self._path)
self._parser = SpecParser(
Path(sourcedir or self.path.parent), macros, force_parse
)
Expand Down Expand Up @@ -101,6 +101,10 @@ def __exit__(
) -> None:
self.save()

@staticmethod
def _read_lines(path: Path) -> List[str]:
return path.read_text(encoding="utf8", errors="surrogateescape").splitlines()

@property
def path(self) -> Path:
"""Path to the spec file."""
Expand Down Expand Up @@ -154,11 +158,11 @@ def rpm_spec(self) -> rpm.spec:

def reload(self) -> None:
"""Reload the spec file content."""
self._lines = self.path.read_text().splitlines()
self._lines = self._read_lines(self.path)

def save(self) -> None:
"""Save the spec file content."""
self.path.write_text(str(self))
self.path.write_text(str(self), encoding="utf8", errors="surrogateescape")

def expand(
self,
Expand Down

0 comments on commit c5dce21

Please sign in to comment.