Skip to content

Handle closed standard input gracefully #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ jobs:
run: |
set -o xtrace
./run-tests
git clone https://github.com/nickboucher/trojan-source
git clone https://github.com/nickboucher/trojan-source ~/trojan-source
./unicode-testscript
13 changes: 12 additions & 1 deletion run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@ NO_COLOR="" COLORTERM="" TERM="xterm-direct" "${pytest[@]}" "${@}"
find . -type f -name "*.py" -print0 | xargs -0 "${pylint[@]}"
"${mypy[@]}" .

utils=(stprint stecho stcat stcatn sttee stsponge)
stdin_utils_to_stdout=(stcat stcatn)
stdin_utils_to_file=(sttee stsponge)
stdin_utils=("${stdin_utils_to_stdout[@]}" "${stdin_utils_to_file[@]}")
utils=(stprint stecho "${stdin_utils[@]}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
utils=(stprint stecho "${stdin_utils[@]}")
utils=(stprint stecho "${stdin_utils_to_stdout[@]}" "${stdin_utils_to_file[@]}")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than this change, I think this is ready to merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

cd "${git_toplevel}/usr/bin"
"${black[@]}" -- "${utils[@]}"
"${pylint[@]}" -- "${utils[@]}"
for file in "${utils[@]}"; do
"${mypy[@]}" -- "${file}"
done

for util in "${stdin_utils_to_stdout[@]}"; do
./"${util}" <&-
./"${util}" - <&-
done
for util in "${stdin_utils_to_file[@]}"; do
./"${util}" <&-
done
24 changes: 18 additions & 6 deletions usr/lib/python3/dist-packages/stdisplay/stcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@

"""Safely print stdin or file to stdout."""

from fileinput import input as file_input
from sys import stdin, stdout, modules
from pathlib import Path
from sys import argv, stdin, stdout, modules
from stdisplay.stdisplay import stdisplay


def main() -> None:
"""Safely print stdin or file to stdout."""
# https://github.com/pytest-dev/pytest/issues/4843
if "pytest" not in modules:
if "pytest" not in modules and stdin is not None:
stdin.reconfigure(errors="ignore") # type: ignore
## File input reads stdin when no file is provided or file is '-'.
for untrusted_text in file_input(encoding="ascii", errors="replace"):
stdout.write(stdisplay(untrusted_text))
if len(argv) == 1:
if stdin is not None:
for untrusted_line in stdin:
stdout.write(stdisplay(untrusted_line))
stdout.flush()
return
for untrusted_arg in argv[1:]:
if untrusted_arg == "-":
if stdin is not None:
for untrusted_line in stdin:
stdout.write(stdisplay(untrusted_line))
else:
path = Path(untrusted_arg)
untrusted_text = path.read_text(encoding="ascii", errors="replace")
stdout.write(stdisplay(untrusted_text))
stdout.flush()


Expand Down
28 changes: 20 additions & 8 deletions usr/lib/python3/dist-packages/stdisplay/stcatn.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,34 @@
(trim trailing whitespace, ensure final newline).
"""

from fileinput import input as file_input
from sys import stdin, stdout, modules
from pathlib import Path
from sys import argv, stdin, stdout, modules
from stdisplay.stdisplay import stdisplay


def main() -> None:
"""
main
Safely print stdin or file to stdout with tweaks
(trim trailing whitespace, ensure final newline).
"""
# https://github.com/pytest-dev/pytest/issues/4843
if "pytest" not in modules:
if "pytest" not in modules and stdin is not None:
stdin.reconfigure(errors="ignore") # type: ignore

for line in file_input(encoding="ascii", errors="replace"):
stdout.write(stdisplay(line).rstrip() + "\n")

if len(argv) == 1:
if stdin is not None:
for untrusted_line in stdin:
stdout.write(stdisplay(untrusted_line).rstrip() + "\n")
stdout.flush()
return
for untrusted_arg in argv[1:]:
if untrusted_arg == "-":
if stdin is not None:
for untrusted_line in stdin:
stdout.write(stdisplay(untrusted_line).rstrip() + "\n")
else:
path = Path(untrusted_arg)
untrusted_text = path.read_text(encoding="ascii", errors="replace")
stdout.write(stdisplay(untrusted_text).rstrip() + "\n")
stdout.flush()


Expand Down
7 changes: 4 additions & 3 deletions usr/lib/python3/dist-packages/stdisplay/stsponge.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
def main() -> None:
"""Safely print stdin to stdout or file."""
# https://github.com/pytest-dev/pytest/issues/4843
if "pytest" not in modules:
if "pytest" not in modules and stdin is not None:
stdin.reconfigure(errors="ignore") # type: ignore
untrusted_text_list = []
for untrusted_text in stdin:
untrusted_text_list.append(untrusted_text)
if stdin is not None:
for untrusted_text in stdin:
untrusted_text_list.append(untrusted_text)
if len(argv) == 1:
stdout.write(stdisplay("".join(untrusted_text_list)))
stdout.flush()
Expand Down
11 changes: 6 additions & 5 deletions usr/lib/python3/dist-packages/stdisplay/sttee.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
def main() -> None:
"""Safely print stdin to stdout and file."""
# https://github.com/pytest-dev/pytest/issues/4843
if "pytest" not in modules:
if "pytest" not in modules and stdin is not None:
stdin.reconfigure(errors="ignore") # type: ignore
untrusted_text_list = []
for untrusted_text in stdin:
untrusted_text_list.append(untrusted_text)
stdout.write(stdisplay(untrusted_text))
stdout.flush()
if stdin is not None:
for untrusted_text in stdin:
untrusted_text_list.append(untrusted_text)
stdout.write(stdisplay(untrusted_text))
stdout.flush()
if len(argv) > 1:
for file_arg in argv[1:]:
with open(file_arg, mode="w", encoding="ascii") as file:
Expand Down
Loading