Skip to content

Commit aa913ec

Browse files
committed
fix compatibility layer code
1 parent c19983f commit aa913ec

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

pyprland/aioops.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,34 @@
22

33
__all__ = ["aiopen", "aiexists", "ailistdir"]
44

5+
import contextlib
56
import io
67

78
try:
9+
import aiofiles.os
810
from aiofiles import open as aiopen
911
from aiofiles.os import listdir as ailistdir
10-
from aiofiles.path import exists as aiexists
12+
13+
aiexists = aiofiles.os.path.exists
1114
except ImportError:
1215
import os
1316

14-
async def aiopen(*args, **kwargs) -> io.TextIOWrapper:
15-
"""Async > sync wrapper."""
16-
f = open(*args, **kwargs) # noqa
17-
_orig_readlines = f.readlines
17+
class AsyncFile:
18+
def __init__(self, file: io.TextIOWrapper):
19+
self.file = file
20+
21+
async def readlines(self) -> list[str]:
22+
return self.file.readlines()
23+
24+
async def __aenter__(self):
25+
return self
1826

19-
async def _new_readlines(*args, **kwargs) -> list[str]:
20-
return _orig_readlines(*args, **kwargs)
27+
async def __aexit__(self, exc_type, exc_val, exc_tb):
28+
self.file.close()
2129

22-
f.readlines = _new_readlines
23-
return f
30+
@contextlib.asynccontextmanager
31+
async def aiopen(*args, **kwargs) -> AsyncFile:
32+
yield AsyncFile(open(*args, **kwargs))
2433

2534
async def aiexists(*args, **kwargs) -> bool:
2635
"""Async > sync wrapper."""

pyprland/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Package version."""
22

3-
VERSION = "2.3.7-3"
3+
VERSION = "2.3.7-4"

0 commit comments

Comments
 (0)