Skip to content

Commit

Permalink
Update internal util module
Browse files Browse the repository at this point in the history
  • Loading branch information
gerlero committed Apr 9, 2024
1 parent 8bb873d commit 9ace894
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions foamlib/_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def run(
)
except CalledProcessError as e:
raise RuntimeError(
f"{e.cmd} failed with return code {e.returncode}\n{e.stderr.decode()}"
f"{e.cmd} failed with return code {e.returncode}\n{e.stderr}"
) from None

else:
Expand Down Expand Up @@ -533,7 +533,7 @@ async def run(
)
except CalledProcessError as e:
raise RuntimeError(
f"{e.cmd} failed with return code {e.returncode}\n{e.stderr.decode()}"
f"{e.cmd} failed with return code {e.returncode}\n{e.stderr}"
) from None

else:
Expand Down
16 changes: 10 additions & 6 deletions foamlib/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def is_sequence(


CalledProcessError = subprocess.CalledProcessError
CompletedProcess = subprocess.CompletedProcess[str]


def run_process(
Expand All @@ -42,7 +43,7 @@ def run_process(
check: bool = True,
cwd: Union[None, str, Path] = None,
env: Union[None, Mapping[str, str]] = None,
) -> "subprocess.CompletedProcess[bytes]":
) -> CompletedProcess:
shell = not is_sequence(cmd)

if sys.version_info < (3, 8):
Expand All @@ -55,7 +56,9 @@ def run_process(
cmd,
cwd=cwd,
env=env,
capture_output=True,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.PIPE,
text=True,
shell=shell,
check=check,
)
Expand All @@ -69,13 +72,13 @@ async def run_process_async(
check: bool = True,
cwd: Union[None, str, Path] = None,
env: Union[None, Mapping[str, str]] = None,
) -> "subprocess.CompletedProcess[bytes]":
) -> CompletedProcess:
if not is_sequence(cmd):
proc = await asyncio.create_subprocess_shell(
str(cmd),
cwd=cwd,
env=env,
stdout=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.PIPE,
)

Expand All @@ -86,15 +89,16 @@ async def run_process_async(
*cmd,
cwd=cwd,
env=env,
stdout=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.PIPE,
)

stdout, stderr = await proc.communicate()

assert stdout is None
assert proc.returncode is not None

ret = subprocess.CompletedProcess(cmd, proc.returncode, stdout, stderr)
ret = CompletedProcess(cmd, proc.returncode, None, stderr.decode())

if check:
ret.check_returncode()
Expand Down

0 comments on commit 9ace894

Please sign in to comment.