Skip to content

Commit

Permalink
raise encoding error in editor when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Mar 11, 2024
1 parent 90fa4ee commit 1369e86
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cat_win/util/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ def _action_save(self, write_func) -> bool:
"""
content = self.line_sep.join(self.window_content)
try:
write_func(content, self.file, self.file_encoding)
# encode here to potentially trigger the unicodeerror event
write_func(content.encode(self.file_encoding), self.file, self.file_encoding)
self.changes_made = True
self.unsaved_progress = False
self.error_bar = ''
Expand Down
14 changes: 9 additions & 5 deletions cat_win/util/stdinhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import sys


def write_file(content: str, src_file: str, file_encoding: str) -> str:
def write_file(content, src_file: str, file_encoding: str) -> str:
"""
Writes content into a generated temp-file.
Parameters:
content (str):
content (str|bytes):
the content to write in a file
src_file (str):
a string representation of a file (-path)
Expand All @@ -22,9 +22,13 @@ def write_file(content: str, src_file: str, file_encoding: str) -> str:
src_file (str):
the path to the temporary file written
"""
if not isinstance(content, str): # in case the content is of types bytes
content = content.decode(file_encoding)
with open(src_file, 'w', encoding=file_encoding, errors='replace') as raw_f:
if isinstance(content, str):
with open(src_file, 'w', encoding=file_encoding, errors='replace') as file:
file.write(content)
return src_file
# in case the content is of types bytes:
# important for the editor, so the encoding errors do not get replaced!
with open(src_file, 'wb') as raw_f:
raw_f.write(content)
return src_file

Expand Down

0 comments on commit 1369e86

Please sign in to comment.