Skip to content

Commit

Permalink
fix false warning on self piping
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Sep 28, 2024
1 parent 531fc12 commit e151a86
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cat_win/src/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,17 @@ def edit_content(content: list, file_index: int = 0, line_offset: int = 0) -> No
line_offset (int):
the offset for counting the line numbers (used in the repl)
"""
if not (content or os.isatty(sys.stdout.fileno()) or file_index < 0):
if not (
content or
os.isatty(sys.stdout.fileno()) or
file_index < 0 or
u_files.is_temp_file(file_index)
):
# if the content of the file is empty, we check if maybe the file is its own pipe-target.
# an indicator would be if the file has just been modified to be empty (by the repl).
# also the stdout cannot be atty.
# in this case the stdout cannot be atty.
# also the repl would not produce this problem.
# also the temp-files (stdin, echo, url, ...) are (most likely) safe.
# an indicator would be if the file has just been modified to be empty (by the shell).
# checking if the file is an _unknown_file is not valid, because by using '--stdin'
# the stdin will be used to write the file
file_mtime = get_file_mtime(u_files[file_index].path)
Expand Down
14 changes: 14 additions & 0 deletions cat_win/src/domain/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ def get_file_display_name(self, file: str) -> str:
return f"<URL {display_url}>"
return file

def is_temp_file(self, file_index: int) -> bool:
"""
determine if a file is a temporary file.
Parameters_
file_index (int):
the index of the file in self.files
Returns:
(bool):
indicates if the file is a temp_file.
"""
return self.files[file_index].path in self.temp_files.values()

def set_files(self, files: list) -> None:
"""
set the files to display.
Expand Down

0 comments on commit e151a86

Please sign in to comment.