Skip to content

Commit 2d63ee1

Browse files
committed
Ditch using cat, just use aiofiles
1 parent ba760a9 commit 2d63ee1

File tree

2 files changed

+5
-22
lines changed

2 files changed

+5
-22
lines changed

runtimepy/util.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,23 @@
33
"""
44

55
# built-in
6-
import asyncio
76
from os import sep
87
from pathlib import Path
9-
from shutil import which
10-
from typing import Iterator, Optional, Union
8+
from typing import Iterator, Union
119

1210
# third-party
1311
import aiofiles
1412
from vcorelib.paths import normalize
1513

1614
ROOT_PATH = Path(sep)
17-
USE_CAT: Optional[bool] = None
1815

1916

20-
async def read_binary(path: Path, use_aiofiles: bool = False) -> bytes:
17+
async def read_binary(path: Path) -> bytes:
2118
"""An async wrapper for reading file contents."""
2219

23-
global USE_CAT # pylint: disable=global-statement
24-
if USE_CAT is None:
25-
USE_CAT = which("cat") is not None
26-
27-
# Avoid ballooning a thread pool with one-off reads.
28-
if USE_CAT and not use_aiofiles:
29-
proc = await asyncio.create_subprocess_exec(
30-
"cat", str(path), stdout=asyncio.subprocess.PIPE
31-
)
32-
result, _ = await proc.communicate()
33-
34-
else:
35-
async with aiofiles.open(path, mode="rb") as path_fd:
36-
result = await path_fd.read()
37-
38-
return result
20+
async with aiofiles.open(path, mode="rb") as path_fd:
21+
result = await path_fd.read()
22+
return result # type: ignore
3923

4024

4125
def normalize_root(*src_parts: Union[str, Path]) -> Path:

tests/test_util.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ async def test_read_binary():
1717
"""Test 'read_binary' invocations."""
1818

1919
assert await read_binary(resource("test.txt"))
20-
assert await read_binary(resource("test.txt"), use_aiofiles=True)

0 commit comments

Comments
 (0)