From 9342186b220790ce1ea9b87bc877d9ec2ad86408 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 23 Jan 2025 12:49:40 +0000 Subject: [PATCH] tests: Fix unclosed file in fd_check script --- docs/changelog.rst | 1 + tests/data/fd_check.py | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 5167ef26..d1edc1b5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -25,6 +25,7 @@ In progress (unreleased) * :gh:issue:`1111` :mod:`mitogen`: Replace uses of deprecated :py:func:`pkgutil.find_loader` * :gh:issue:`1213` :mod:`mitogen`: Fix unclosed file in first stage +* :gh:issue:`1213` tests: Fix unclosed file in fd_check script v0.3.21 (2025-01-20) diff --git a/tests/data/fd_check.py b/tests/data/fd_check.py index 0a87a95e..f3684443 100755 --- a/tests/data/fd_check.py +++ b/tests/data/fd_check.py @@ -26,6 +26,7 @@ def controlling_tty(): return None +out_path = sys.argv[1] fd = int(sys.argv[2]) st = os.fstat(fd) @@ -35,7 +36,7 @@ if sys.argv[3] == 'write': else: buf = os.read(fd, 4).decode() -open(sys.argv[1], 'w').write(repr({ +output = repr({ 'buf': buf, 'flags': fcntl.fcntl(fd, fcntl.F_GETFL), 'st_mode': st.st_mode, @@ -43,4 +44,21 @@ open(sys.argv[1], 'w').write(repr({ 'st_ino': st.st_ino, 'ttyname': ttyname(fd), 'controlling_tty': controlling_tty(), -})) +}) + +try: + out_f = open(out_path, 'w') +except Exception: + exc = sys.exc_info()[1] + sys.stderr.write("Failed to open %r: %r" % (out_path, exc)) + sys.exit(1) + +try: + out_f.write(output) +except Exception: + out_f.close() + exc = sys.exc_info()[1] + sys.stderr.write("Failed to write to %r: %r" % (out_path, exc)) + sys.exit(2) + +out_f.close()