From f4ffeedc73241dac963b5e6b03a54d80bc29e858 Mon Sep 17 00:00:00 2001 From: Sam Hartman Date: Thu, 9 Jan 2025 13:54:04 -0700 Subject: [PATCH 1/2] async return_cmd should raise on failure Commit 3a9d8ce9fb incorrectly suppresses exceptions when a return_cmd True RunningCommand is awaited. Explicitly check for exceptions so they are properly raised. --- sh.py | 3 +++ tests/sh_test.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/sh.py b/sh.py index b02d6e16..8f8f3c6b 100644 --- a/sh.py +++ b/sh.py @@ -890,6 +890,9 @@ def __await__(self): async def wait_for_completion(): await self.aio_output_complete.wait() if self.call_args["return_cmd"]: + # We know the command has completed already, + # but need to catch exceptions + self.wait() return self else: return str(self) diff --git a/tests/sh_test.py b/tests/sh_test.py index 0fe03111..04198069 100644 --- a/tests/sh_test.py +++ b/tests/sh_test.py @@ -1802,6 +1802,19 @@ async def main(): asyncio.run(main()) + def test_async_return_cmd_exc(self): + py = create_tmp_test( + """ +import sys +sys.exit(1) +""" + ) + + async def main(): + await python(py.name, _async=True, _return_cmd=True) + + self.assertRaises(sh.ErrorReturnCode_1, asyncio.run, main()) + def test_handle_both_out_and_err(self): py = create_tmp_test( """ From 11f55d5db6df0ce8571268a1e6c1179d88a1e9ed Mon Sep 17 00:00:00 2001 From: Andrew Moffat Date: Thu, 9 Jan 2025 14:21:33 -0800 Subject: [PATCH 2/2] changelog and v2.2.1 --- CHANGELOG.md | 4 ++++ pyproject.toml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f705b552..0eae0cfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.2.1 - 1/9/25 + +- Bugfix where `async` and `return_cmd` does not raise exceptions [#746](https://github.com/amoffat/sh/pull/746) + ## 2.2.0 - 1/9/25 - `return_cmd` with `await` now works correctly [#743](https://github.com/amoffat/sh/issues/743) diff --git a/pyproject.toml b/pyproject.toml index 6621570b..af035490 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sh" -version = "2.2.0" +version = "2.2.1" description = "Python subprocess replacement" authors = ["Andrew Moffat "] readme = "README.rst"